0

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
});
2
  • Why are you using .index()? Commented Apr 22, 2012 at 20:22
  • Since .index() is not the thing. I could just as well state .dummy() Commented Apr 22, 2012 at 20:36

2 Answers 2

2

You can parse it out of the name attribute like this:

$("select[name^='option[group]']").change(function(){
    var nums = this.name.match(/\[(\d+)\]$/)
    if (nums) {
        alert('Index:' + nums[1]);
    }
  // Do something with option values of group
});

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:

$("select[name^='option[group]']").change(function(){
    var nums = this.name.match(/\[([^\]]+)\]$/)
    if (nums) {
        alert('Index:' + nums[1]);
    }
  // Do something with option values of group
});

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:

var selectGroups$ = $("select[name^='option[group]']");
selectGroups$.change(function(){
    var index = selectGroups$.index(this);
    // index will be a number like 0, 1, 2, 3 for which
    // sequential "select[name^='option[group]']" we are operating on.
});
Sign up to request clarification or add additional context in comments.

6 Comments

That would only work for numeric index keys. Is there no conventional way of doing this? Like $(this).parent.something(...)
@tim: I don't speak for jfriend00, but IMO, the conventional way is to be a programmer, and write a little code that does what you need.
@tim - the data is ONLY available inside the name string. As far as the browser is concerned the name is just a string. The [xxx] is a convention in the name that gets used by other parts of the process. If you want to parse out whatever is in the last [] in the name, then the regex can be change to take any sequence of characters. I've added that option to my answer too.
Is it smarter to get the nth key index rather than the key index name? I later need to pass this on to the corresponding group of option value.
I don't know what "nth key index" is. Are you now asking a different question? If so, please explain what that is. If you're trying to find which group number sequentially on the page you're in, I've added that as a third option to my answer.
|
-1

This does not directly answer the question but I accomplished chained select fields like this:

$("select[name^='option[group]']").change(function(){
    var valueField = this.name.replace(/group/, 'value');
    $("select[name='"+ valueField +"']").somefunction(...);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.