Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
193
rated 0 times [  196] [ 3]  / answers: 1 / hits: 21946  / 16 Years ago, tue, march 24, 2009, 12:00:00

I have to hidden input fields such as:



<input name=foo value=bar>
<input name=foo1 value=bar1>


I'd like to retrieve both of those values and POST them to the server using jQuery. How does one use the jQuery selector engine to grab those values?


More From » jquery

 Answers
33

As foo and foo1 are the name of you input fields, you will not be able to use the id selector of jQuery (#), but you'll have to use instead the attribute selector :



var foo = $([name='foo']).val();
var foo1 = $([name='foo1']).val();


That's not the best option, performance-wise. You'd better set the id of your input fields and use the id selector (e.g. $(#foo)) or at least provide a context to the attribute selector:



var form = $(#myForm); // or $(form), or any selector matching an element containing your input fields
var foo = $([name='foo'], form).val();
var foo1 = $([name='foo1'], form).val();

[#99801] Wednesday, March 18, 2009, 16 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
khalidkendelld

Total Points: 55
Total Questions: 99
Total Answers: 77

Location: South Korea
Member since Tue, Feb 22, 2022
2 Years ago
;