Can someone please let me know how to get values from several input fields?
I have a list with several inputs like this:
<li>
<label>Additional Title: </label><input type='text' name='additionaltitlename' ... />
</li>
<li>
<label>Additional Title: </label><input type='text' name='additionaltitlename' ... />
</li>
I have a solution in Javascript (on form submit):
...
var extratitles = document.getElementsByName('additionaltitlename');
var str = '';
for (var i = 0; i < extratitles.length; i++) {
str = str + '|' + extratitles.item(i).value;
}
}
How do I do the same thing in JQuery?
It’s not valid to have two inputs of the same name. If you want to do this, you can use
<input name="titles[]">You can try this:
With this jQuery
See it working here on jsFiddle
EDIT
This answer gives you the titles in an array instead of a string using a
|separator. Personally, I think this is a lot more usable.If you’re just submitting the form and you want to support multiple values, use the
.serializemethod as described in the other answer