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 8921309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:30:27+00:00 2026-06-15T06:30:27+00:00

I have this ajax autocomplete function that works on every input field with a

  • 0

I have this ajax autocomplete function that works on every input field with a class of .train. However, the suggestions that are being passed into the .suggest_q class are being displayed in every .suggest_q, next to each .train input field, and not just the .train field that is being used. I believe I am using the correct selectors as I am using ‘this’ instead of .train near the .click function but I am still having issues. I want this function to only work on the .suggest_q and .train classes that are being used by the user and to have the other ones not display any suggestions. What would be the best way to have the autocomplete function to work with the .train and .suggest_a that is being typed in?

here is my ajax code for adding new .train input fields:

 <script type="text/javascript">
 $(function() {
        var scntDiv = $('#p_scents');
        var i = $('#p_scents p').size() + 0;

        $('#addScnt').live('click', function() {
                $('<p><input type="text" name="train[]" autocomplete="off" class="train"  /><span class="suggest_q" id="suggest_q"></span><a href="#" id="remScnt">Remove train</a></p>').appendTo(scntDiv);
                i++;
                return false;
        });

        $('#remScnt').live('click', function() {
                if( i > 2 ) {
                        $(this).parents('p').remove();
                        i--;
                }
                return false;
        });
});
</script>

here is the ajax autocomplete code

<script type="text/javascript">
$(document).ready(function() {
    $(".train").each(function(index, element) {
       $(".train").live("keyup", function() { 
        $(".suggest_q").html("");
            var train = $(this).val();   
            train = $.trim(train);
            if(train)
            {
                $.ajax({

                    url: "train_ajax_query.php",
                    data: "train="+train,
                    success: function(msg) {
                        $(".suggest_q").html(msg);
                        $(".suggest_q ul li").mouseover(function() {
                            $(".suggest_q ul li").removeClass("hover");
                            $(this).addClass("hover");
                        })
                        $(".suggest_q ul li").click(function() {
                            var value   =   $(this).html();
                            $(element).val(value);
                            $(".suggest_q ul li").remove();
                        });

                    }
                });
            }
        });
    });

});

</script>

here is the HTML

<div id="p_scents">
    <input type="text" class="train" size="20" autocomplete="off" name="train[]" />
                <div id="suggest_q" class="suggest_q">

                </div>

</div>
  • 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-06-15T06:30:28+00:00Added an answer on June 15, 2026 at 6:30 am

    In your autocomplete code, you use a global selector. You need to save the selected elements and then use these variables.

    //Changed the live call for an on call since live is deprecated.
    $(document).ready(function() {
      $("#p_scents").on("keyup", '.train', function() { 
        //we save vars with the objects we want to work with.
        var selectedTrain = $(this);
        var selectedSuggest = $(this).siblings('.suggest_q');
        selectedSuggest.html("");
        var train = selectedTrain.val();   
        train = $.trim(train);
        if(train) {
          $.ajax({
            url: "train_ajax_query.php",
            data: "train="+train,
            success: function(msg) {
              //the variables here should still be valid, unless you start more than one ajax
              //call simultaneously.
              selectedSuggest.html(msg);
              selectedSuggest.find("ul li").mouseover(function() {
                // This can happen when the variable selectedSuggest is no longer valid
                //for example, when another try has been done on another .train
                $(this).siblings("li").removeClass("hover");
                $(this).addClass("hover");
              });
              selectedSuggest.find("ul li").click(function() {
                var value   =   $(this).html();
                //again, the vars can have changed here, since the click can be a long time 
                //after the event registration
                $(this).parents('.suggest_q').siblings('input').val(value);
                $(this).parents('.suggest_q').find("li").remove();
              });
            }
          });
        }
      });
    });
    

    The code has not been tested, but should work. Leave comments if you find any error, i will edit.

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

Sidebar

Related Questions

I have this code that works well: {livre:empty_name} $.ajax({ url: sent.php, type: post, dataType:
Greetings, I have a jqueryui autocomplete input that uses an ajax call to populate
I have a jQuery autocomplete that works fine making the suggestions but when I
I have this jQuery code: $(document).ready(function() { $.ajax({ url: pages/+page+.php, cache: false }).done(function( html
I have this problem that my sites uses alot of ajax and when a
I'd like to implement a simple AJAX function locally that allows me to autocomplete
I have this jquery ajax code that submits my form if it gets changed.
I have a javascript function that has been driving me nuts. This is the
i have an input using the jquery autocomplete plugin. with this i want to
I have a form with a text field that uses jQuery-UI autocomplete widget. I'm

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.