-1

I'd like to change the selected option with jQuery.

<select name="nameSelect" id="idSelect">
    <option value="0">Text0</option>
    <option value="1">Text1</option>
    <option value="2" selected>TextSelected1</option>
</select>

I'm trying with $("#idSelect").val(); or similar, but it doesn't work.

Regards

oscarvady
  • 450
  • 1
  • 4
  • 12

4 Answers4

2

pass the value which needs to be set as parameter in .val() :

$("#idSelect").val('0');
Milind Anantwar
  • 79,642
  • 23
  • 92
  • 120
1

Add the value in the val as

$("#idSelect").val(0); 
Tushar
  • 13,804
  • 1
  • 24
  • 44
1

That does work, but you need to include the value of the option you want selected as a parameter to val(), e.g.:

$("#idSelect").val('1');
danmullen
  • 2,550
  • 3
  • 19
  • 28
1

You can simply change the value of the select. And I would suggest it to use the select[name=NAME]-Tag..

$('select[name=nameSelect]').val(1);

Greetings from Vienna

Bernd
  • 700
  • 1
  • 5
  • 13
  • 2
    Why would you suggest selecting by `name` instead of `id`? It is slower (significantly, in some cases); if `id` is a possibility then surely use it... – Shai Oct 07 '14 at 11:01
  • Because the code will be more clearer - and you do not need to set an ID. When you make bigger forms which are complicated this is easier and thats why I prefer it. I don't think that it is slower and this wouldn't be noticed! – Bernd Oct 07 '14 at 11:06