How can I select all HTML items by name ending with a name
using jQuery
<input type="text" name="[0].LastName" value="Smith">
<input type="text" name="[1].LastName" value="James">
<input type="text" name="[2].LastName" value="Wilson">
<input type="text" name="[3].LastName" value="Peterson">
For instance if I wanted in jQuery loop through the multiple elements but only select Element Names ending with ].LastName
, therefore it should only return the 3 elements above.
3
Answers
You can use
[attr$=value]
to select elements whoseattr
attribute ends withvalue
.You can use the
ends with
selector:https://api.jquery.com/attribute-ends-with-selector/
e.g:
See comment in the code below. But also, you could just name all of those elements the same
name
and then when the form submits, the data will be combined into a comma delimited string.