Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7618301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:22:07+00:00 2026-05-31T03:22:07+00:00

I have this autocomplete $( "#tags" ) // don’t navigate away from the field

  • 0

I have this autocomplete

    $( "#tags" )
                // don't navigate away from the field on tab when selecting an item
                .bind( "keydown", function( event ) {
                    if ( event.keyCode === $.ui.keyCode.TAB && $( this ).data( "autocomplete" ).menu.active ) {
                        event.preventDefault();
                    }
                })
                .autocomplete({
                    minLength: 2,
                    /*source: function( request, response ) {
                        // delegate back to autocomplete, but extract the last term
                        response( $.ui.autocomplete.filter(
                            availableTags, extractLast( request.term ) ) );
                    },*/
                    source: function( request, response ) {
                        $.getJSON( "json.php", {
                            term: extractLast( request.term )
                        }, response );
                    }
                    

/*all the options omitted*/
            });

Which works great on dom.ready, but if I dynamically remove and add again the same form, the autosuggest feature wont get enabled, so I thought about using .live() but its giving me the same behavior

 $( "#tags" )
                // don't navigate away from the field on tab when selecting an item
                .live( "keydown", function( event ) {
                    if ( event.keyCode === $.ui.keyCode.TAB && $( this ).data( "autocomplete" ).menu.active ) {
                        event.preventDefault();
                    }
                })
                .autocomplete({
                    minLength: 2,
                    /*source: function( request, response ) {
                        // delegate back to autocomplete, but extract the last term
                        response( $.ui.autocomplete.filter(
                            availableTags, extractLast( request.term ) ) );
                    },*/
                    source: function( request, response ) {
                        $.getJSON( "json.php", {
                            term: extractLast( request.term )
                        }, response );
                    }
                    

/*all the options omitted*/
            });

shouldn’t .live() handle the dom change?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T03:22:09+00:00Added an answer on May 31, 2026 at 3:22 am

    Yes, you should use on (although live is still available).
    And you may want to initialize the autocomplete upon the first keystroke:

    $( document ).on( "keydown", "#tags", function( event ) {
        if ( $.type( $( this ).data( "autocomplete" ) ) == "undefined" ) {
            $( this ).autocomplete({
                minLength: 2,
                source: function( request, response ) {
                    $.getJSON( "json.php", {
                        term: extractLast( request.term )
                    }, response );
                }
            });
        }
        if ( event.keyCode === $.ui.keyCode.TAB && $( this ).data( "autocomplete" ).menu.active ) {
                event.preventDefault();
        }
    });
    

    Note: instead of $( document ).on it is preferrable to use the closest “#tags” parent which is always in the DOM (never is removed or reloaded). document is parent of all, and is always present, that’s why it always works for on. But we should avoid attaching all event handlers in the document, if possible.


    Also, what other users answered is correct. When you do:

    $( '#tags' ).live( ... ).autocomplete( ... );
    

    This is the same as:

    $( '#tags' ).live( ... );
    $( '#tags' ).autocomplete( ... );
    

    It means: the live call is not attached to the autocomplete call.


    Also, as other users answered, instead of the solution above, a better place to initialize the autocomplete would be in the AJAX success event handler, i.e., right AFTER the “#tags” element is included in the DOM:

    $('div#something').load('new_content_from_server', function() {
        // the tags arrived! let's initialize the autocomplete
        $( '#tags' ).autocomplete( ... );
    });
    

    I hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently, I have this version of the autocomplete control working when returning XML from
I have an jQuery autocomplete field with non latin characters. My Controller has this
I have this code which is for youtube autocomplete suggestions: $(function(){ $('#input-field').keyup(function(){ var val
I have this code: $(#xyz).unautocomplete().autocomplete(dataVar, { minChars: 0, width: 400, matchContains: true, highlightItem: true,
This other SO question asks about an autocomplete textbox in WPF. Several people have
I have this: $(#term).autocomplete({ minLength: 2, source: function( request, response ) { $.ajax({ url:
I have this code : $(document).ready(function(){ $(#search_input).autocomplete({ source: function(request, response) { $.ajax({ url: 'http://query.yahooapis.com/v1/public/yql',
I have this line on a website <input type=text autocomplete=off value= maxlength=20 class=shipBox name=firstName
I have this version of jQuery-autocomplete: jQuery Autocomplete Mod , It's cool with me
I have this page in which I slightly modified the jQuery autocomplete to make

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.