1

I am new to jquery I have the following field

 <select  name[]="post">
              <option value="op1">op1</option>
              <option value="op2">op2</option>
              <option value="op3">op3</option>
              </option>
 </select>   
 How to get the post[0] & post[1] values in Jquery any Idea 
 . 
3
  • 1
    never seen that notaion name[]="xxx" ar e u sure this is html valid? Commented Jul 12, 2010 at 12:08
  • 1
    Attribute names cannot have [] in them. Commented Jul 12, 2010 at 12:11
  • @helle: it's no valid html. @venkat: what are you trying to archive here? Commented Jul 12, 2010 at 12:12

4 Answers 4

5

you are searching for this...

<select name="my_select" id="foo">
              <option value="op1">op1</option>
              <option value="op2">op2</option>
              <option value="op3">op3</option>
              </option>
 </select> 


var options = $('#foo').find('option');
console.log($(options[0]).attr('value'));
Sign up to request clarification or add additional context in comments.

1 Comment

unless you have activated the developer tools console... it will not work in ff, too if you haven't installed firebug ;-)
0

if you want multiple select option use the key word "multiple"

<select name="my_select" id="foo" multiple>
          <option value="op1">op1</option>
          <option value="op2">op2</option>
          <option value="op3">op3</option>
          </option>
</select>

1 Comment

According to standard, it's: multiple="multiple"
0

You can get Select's selected value in the following easy ways

$('select#your_id').val();

$('select#your_id').children("option:selected").val()

$("select#your_id > option:selected").val()

And to get any option on the bases of index, you can use the following

$("select#your_id > option:nth-child(1)")

Point to note is the index starts from 1-n (0 will return empty)

Comments

0
<select name="test" id="test">
          <option value="op1">op1</option>
          <option value="op2">op2</option>
          <option value="op3">op3</option>
          </option>
</select> 

to get the vallue from the select list you can use this piece of code: output = $('#test').val();

$('#some_button').click(function()  {
  output = $('#test').val(); 
});

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.