beetle
beetle

Reputation: 223

Set selected option

I've got option select. How to set as var selected select automaticly?

<option>
<select>1</select>
<select>2</select>
</option>

E.g. I manually select option 1, and it automaticly is

<select selected="selected">1</select>

EDIT I found out.

<select id="sel">
    <option value="1">aa</option>
    <option value="2">bb</option>
    <option value="3">cc</option>
</select>

$("#sel").change(function(){
   alert($(this).val())
});

Upvotes: 0

Views: 2206

Answers (4)

dapperwaterbuffalo
dapperwaterbuffalo

Reputation: 2748

I don't quite see what your issue is, however im getting the idea from what you've written that you want to submit the form upon the user selecting an option from the dropdown?

In which case u need JQuery to implement a listener (.live()) on the dropdown which will fire an event, in your case you want to submit the form (i think) so perhaps use .post() if you do want to use ajax.

look these up on the jQuery site.

Upvotes: 0

mesimplybj
mesimplybj

Reputation: 639

I agree with @creminsn!! and For that to get value Try this

var val;
$('#selectID').click(function(){
val=$('#selectID option:selected').val();
});

val will give you the selected val.And if you need text you can do

$('#selectID option:selected').text();

Upvotes: 0

ncremins
ncremins

Reputation: 9200

your code should be

<select>
    <option selected="selected">1</option>
    <option>2</option>
</select>

Upvotes: 1

Willem D&#39;Haeseleer
Willem D&#39;Haeseleer

Reputation: 20190

try this

$("#myOption").val("1");

Upvotes: 1

Related Questions