Jump to content
SF23103

alternative to onchange when using autocomplete

Recommended Posts

Hello,

I have a form that is using Google's autocomplete for addresses.  I want the address they entered to appear in a paragraph of text at the bottom of the form. It works great when the user enters text manually.  Unfortunately, when the user selects an autocomplete result option, it does not update the paragraph text with the autocompleted result.  I did some reading and see that is normal, and onkeyup, onchange, etc. don't work with programmatically entered text.  Is there an alternative that can do what I want it to do?

 

<input id="searchTextField" name="Premise_Address" type="text" size="50" placeholder="Enter a location" autocomplete="on" runat="server" onkeyup="getVals(this, 'text');">


<p id="pAddressTextInput"></p>/>  

 

  <script>
        function getVals(formControl, controlType) {
            switch (controlType) {
                case 'text':
                    // Get the value for a text input
                    var value = $(formControl).val();
                    $("#pAddressTextInput").text(value);
                    break;
             }
        }
    </script>

 

Share this post


Link to post
Share on other sites

Have you tried the blur() event on the field? I can't guarantee it'll do what you want (I'm away from my computer right now), but I'm pretty sure that element.onblur() fires when a form field autocompletes as well as when the user navigates away from that field.

Share this post


Link to post
Share on other sites

Thanks for the suggestion!  I just tried changing onkeyup to onblur and got a *similar* result.  The only difference is that with onblur, if you tab or click out of the input box and then tab or click back into it, then it will update.  Getting closer 🙂

Share this post


Link to post
Share on other sites

Ah - yes, blur will occur on focus in and focus out. Forgot about that, but glad you figured out the trigger you needed!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.