2

I am using jQuery auto-complete. I have download this script from http://code.google.com/p/jquery-autocomplete/. I want to use this on multiple fields. Please help me thanks.

$("#input").autocomplete("samefile.php");
$("#input").autocomplete("samefile.php");

thanks

2
  • 2
    Did you have a question? Commented Jan 5, 2011 at 20:29
  • By the way, you typically do not need to loop through elements with JQuery, as it does this for you. For instance one line stating $("input").autocomplete("samefile.php"); would add autocomplete to all input elements currently on the page. Commented Jan 5, 2011 at 22:34

2 Answers 2

2

the hash mark means you are using IDs to select elements. there should however never be more than one element in your page with the same ID. for instance,

<input id="test" /><input id="test" />

is invalid HTML.

The second problem, is that it appears you are trying to find tag names, which means you should simply leave out the hash mark from your code, and JQuery will apply your methods to all of the tags with that tag name,

$("input").autocomplete("samefile.php");

will apply autocomplete to all input tags on your page.

Third, I would use classes instead of tag names incase you ever want to have an input on your page that does not use the same auto complete. So your html would look like this,

<input class="auto" /><input class="auto" />

and your JQuery would look like this.

$(".auto").autocomplete("samefile.php);

I also wonder where you are calling your JQuery from?

Sign up to request clarification or add additional context in comments.

4 Comments

thanks for reply. I'm calling Jquery from js folder (js/jquery.min.js)
i meant where is the $("#input").autocomplete("samefile.php"); line?
Thanks for reply. Its working. I have one for question. Is there any way using loop instead of defining class?
Yes. JQuery has an each() method. See this link for more details: api.jquery.com/each
2

You should use a less specific selector to mark multiple fields as autocomplete in one statement. Maybe you can assign a class of type ".autocomplete" and then use that.

<input type=textbox" name="txt1" class="autocomplete"/>
<input type=textbox" name="txt2" class="autocomplete"/>

 $(".autocomplete").autocomplete("samefile.php"); 

2 Comments

There's a typo in your answer, you didn't close the last double quote after samefile.php.
Thx.. I think a historical problem of copy paste from question :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.