0

I tried to select a multidimensional form item with JQuery. But when I try to alert its value I just get an undefined.

This is my form:

    <select name='item[1][name]'>
       <option value='1'>Name 1</option>
       <option value='2'>Name 2</option>
       <option value='3'>Name 3</option>
       <option value='4'>Name 4</option>
    </select>

    <input type='text' name='item[1][id]' class='text' />

When I click on submit button, I try to select all my form elements.

I successfully selected the text input field with:

var item1i = $('input[name="item[1][id]"]');
var personi = $(item1i).val();

But when I try to select the select tag like this:

var item1n = $('input[name="item[1][name]"]');
var personn = $(item1n).val();

When I alert personi the right value is shown. But when I alert personn I get undefined.

Any idea what I'm doing wrong?

1
  • 2
    You're trying to select the "select" tag with an "input" selector. Commented Apr 1, 2013 at 15:09

2 Answers 2

5

Use $('select[name="item[1][name]"]');

Or alternatively, $(':input[name="item[1][name]"]');

input will only select the elements with input tag, so for your select you have to use the select tag.

Alternatively jquery provides the special :input selector that returns any form element (input texts, selects, checkboxes, textarea,etc), so you can use that too.

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

1 Comment

don't mind that I tried to solve this problem like for two hours and didn't even see that i use input for a select element.
0
var item1n = $('input[name="item[1][name]"]');  

you've the wrong selector here. Try this

var item1n = $('select[name="item[1][name]"]');

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.