How do I make alert() return either 123, 456 or the nth group key?
I wanna retrieve the index key is to do some chain operations on the corresponding option values.
Note: The index keys could just as well be alphanumeric.
HTML:
<select name="option[group][123]">
<option>a</option>
<option>b</option>
</select>
<select name="option[value][123]">
<option>1</option>
<option>2</option>
</select>
<select name="option[group][456]">
<option>a</option>
<option>b</option>
</select>
<select name="option[value][456]">
<option>1</option>
<option>2</option>
</select>
Script:
$("select[name^='option[group]']").change(function(){
alert('Index:' + $(this).fieldsetIndexKeyIdentifier());
// Do something with option values of group
});
You can parse it out of the name attribute like this:
Working demo here: http://jsfiddle.net/jfriend00/TcRzW/
Or, if you want any sequence of characters from the last index in the name, it would be this:
Working demo here: http://jsfiddle.net/jfriend00/TcRzW/
If you now want to know which group number you’re working on, you can do that like this: