-2

How do I get the value of option 3 in select name period with jQuery?

In Javascript, I use document.form.period.value to access the value when button is clicked

<form name="form"> <select name="link"> <option value="1">$1000</option> <option value="2">$2000</option> <option value="3">$3000</option> <option value="4">$4000</option> <option value="5">$5000</option> </select> <input type="button" onclick="alert(document.form.link.value)" value="alert"> <select name="period"> <option value="1">1 day</option> <option value="2">2 day</option> <option value="3">3 day</option> <option value="4">4 day</option> <option value="5">5 day</option> </select> <input type="button" onclick="alert(document.form.period.value)" value="alert"> </form> 
1
  • 1
    Could you rephrase your question? Do you want "selected value" or "third option value"? Commented Aug 1, 2014 at 18:21

1 Answer 1

3

I am ashamed to answer this question, but:

$('select[name="period"] > option:nth-child(3)') // get the third option element $('[name="period"]').val() // get the value of the selected option 
Sign up to request clarification or add additional context in comments.

11 Comments

"I am ashamed to answer this question" is the best part of this answer. It doesn't do what the OP wants. He wants to access the selected value not just the 3rd one.
OP didn't say that it will be the selected option
+ 1 This is almost bang on, just missing the val(). Can't believe it was down voted so fast
@YuryTarabanko if you use .val it will return the value of the selected element. This guy wants to get the value of the third option, not the value of the selected option.
@MarcoBonelli according to document.form.period.value it is that what OP wants :)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.