1

I've got a form with several multi-dimensional names. I am using the jquery *= selector to pick-out groups on them and perform functions when they change. I am trying to figure out how to get the individual array indexes of the form element name.

I want to be able to identify the doorID by it's individual array 'parts'. e.g door_6x7, or 0 for opening[door_6x7][0]

JQUERY:

    $("select[name*=door_6x]").change(function() {

        var doorID = $(this).attr("name");
            alert(doorID);

    });

SAMPLE HTML:

                  <td width="50"><select name="opening[door_6x7][0]">
                      <option value="0" selected="selected">0</option>
                      <option value="1">1</option>
                      <option value="2">2</option>
                      <option value="3">3</option>
                    </select></td>
                  <td width="50"><select name="opening[door_6x7][1]">
                      <option value="0" selected="selected">0</option>
                      <option value="1">1</option>
                      <option value="2">2</option>
                      <option value="3">3</option>
                    </select></td>
                  <td width="50"><select name="opening[door_6x7][2]">
                      <option value="0" selected="selected">0</option>
                      <option value="1">1</option>
                      <option value="2">2</option>
                      <option value="3">3</option>
                      <option value="4">4</option>
                      <option value="5">5</option>
                      <option value="6">6</option>
                    </select></td>
                  <td width="50"><select name="opening[door_6x7][3]">
                      <option value="0" selected="selected">0</option>
                      <option value="1">1</option>
                      <option value="2">2</option>
                      <option value="3">3</option>
                      <option value="4">4</option>
                      <option value="5">5</option>
                      <option value="6">6</option>
                    </select></td>
1
  • interesting... I never even knew this is possible... would like to see an answer for it, too :) Commented Feb 20, 2011 at 21:20

1 Answer 1

1

I'm not sure if this is what you want:

 $("select[name*=door_6x]").change(function() {

    var doorID = $(this).attr("name");
    var x = doorID.match(/\[[0-9]+\]/i);

    alert(x[0].match(/[0-9]+/i));

});
Sign up to request clarification or add additional context in comments.

2 Comments

the regex works good to parse it manaully. I was hoping there was a way to treat the name as an array so I could reference opening[0] would be 'door_6x7' and opening[1] would be '0'. Maybe I'm just thinking about it the wrong way.
Why don't you use a rel attribute and then just read the rel? I usually do it like that..

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.