Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery select option with button

Is it possible to select special <option> item with jQuery by pressing button?

For example :

<a id="item1" href="#">Item 1</a>
<a id="item2" href="#">Item 2</a>
<a id="item3" href="#">Item 3</a>

<form>
.......
<select id="items" name="items">
<option value="1">I dont know</option>
<option value="2">Item 1</option>
<option value="3">Item 2</option>
<option value="4">Item 3</option>
</select>
.........

</form>

If you press the second button than in form automatically selects second option. Is it possible?

like image 342
iKaspars Avatar asked Sep 04 '26 12:09

iKaspars


1 Answers

This is a simple jQuery way of doing it.

$('a').click(function() {
    var select_num = $(this).index() + 1;
    $('#items option').eq(select_num).attr('selected', 'selected');
});

Here is the example fiddle.

like image 117
Phil Avatar answered Sep 07 '26 10:09

Phil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!