Get input name array key in JQuery

By: Ryan Wong at

When you want to retrieve the key value in an input element that has a name array you need to do the following:

1
<input type="text" name="tag[1]" value="blah"/>


1
2
3
4
5
6
7
var tagSelect = [];
$('input[name^="tag"]').each(function(oneTag){
var oneValue = $(this).val();
var name = $(this).attr('name');
var id = parseInt(name.match(/[0-9]+/));
tagSelect.push(id);
});

Hope it helps you out.