Reputation: 26762
I have a set of input fields on my page. They're setup as an array like so:
<input type="text" name="test[name][]" /><br />
<input type="text" name="test[name][]" /><br />
<input type="text" name="test[name][]" /><br />
<input type="text" name="test[name][]" />
What i need to do next it to set a unique value in each textfield. But i don't know how to iterate over these fields with jQuery. My attempt failed: DEMO
$(function() {
$.each('input[name="test[name][]"]', function() {
$(this).val('blaat');
});
});
Any idea how i can iterate over each input field, selecting them by name!? I don't have any influence on these controls. So i cannot give them an extra class name or anything like that. All i have are their names.
Upvotes: 2
Views: 6033
Reputation: 148110
The selector you use to get array just a is string but not array
'input[name^="test"]'
should be $('input[name="test[name][]"]')
You can do it this way,
$(function() {
$.each($('input[name^="test[name][]"]'), function() {
$(this).val('blaat');
});
});
Upvotes: 3
Reputation: 2760
You could do something like
$(function() {
$.each($('input[name^="test"]'), function() {
$(this).val('blaat');
});
});
EDIT: More efficient
$(function() {
$('input[name^="test"]').each(function() {
$(this).val('blaat');
});
});
Upvotes: 0