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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:37:33+00:00 2026-06-13T04:37:33+00:00

I am trying to get autocomplete working on any div with the id=train however,

  • 0

I am trying to get autocomplete working on any div with the id=”train” however, it seems to only be working on the first input field with a class of “train”

javascript code:

<script type="text/javascript">
$(document).ready(function() {
    $(".train").each(function() {
        $(".train").keyup(function() {
        $("#suggest_q").html("");
            var train = $(".train").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();
                            $("#train").val(value);
                            $("#suggest_q ul li").remove();
                        });

                    }
                });
            }
        });
    });

});

</script>

HTML code:

    <form id="train_create_form" name="train" action="submit_train.php" method="POST">


    <input type="text" id="train" class="train" size="20" name="train[]" placeholder="Train 1" />
        <div id="suggest_q">

        </div>
    <input type="text" id="train" size="20" class="train" name="train[]" placeholder="Train 2" />
        <div id="suggest_q">

        </div>

  <input id= "submit_train" type="submit" name="submit_train" value="Submit All trains">
        </form>

I found this old post but I am not sure how to adopt the fix for my problem 🙁

add more input fields code:

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

        $('#addScnt').live('click', function() {
                $('<p><input type="text" id="train" name="train[]" class="train" placeholder="Train ' + i +'" /><div id="suggest_q"></div><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>
  • 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-13T04:37:34+00:00Added an answer on June 13, 2026 at 4:37 am

    Beause here:

        $(".train").each(function() {
            $(".train").keyup(function() {
    

    you get every time only first field 🙂 try:

        $(".train").each(function(index, element) {
            element.keyup(function() {
                // or even: $(element).keyup(function() {
    

    References:
    Each
    KeyUp
    Val

    I copypaste whole example:

    <script type="text/javascript">
    $(document).ready(function() {
        $(".train").each(function(index, element) {
            $(element).keyup(function() {
            $("#suggest_q").html("");
                var train = $(element).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();
                            });
    
                        }
                    });
                }
            });
        });
    
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to get autocomplete working working with JQuery UI, but I am
I am trying to get an autocomplete working and making an Ajax form submission
I'm trying to get an autocomplete form working, and I can't seem to figure
I've been trying to get the Zend Jquery autocomplete function working, when i noticed
Im trying to get autocomplete-rails.js working in Rails with Ajax, i Have the following
I'm trying to get jQuery autocomplete working. The problem is that my json array
Trying to get an autocomplete to work an a textbox but it seems that
So i am trying get 2 div-containers which both should contain centered text (Both
Trying to get a wildcard search to pick up on any text in org_name
I am trying to get AutoComplete to work on a website application I am

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.