-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
  • 81,290
  • 25
  • 94
  • 125
1

Add the value in the val as

$("#idSelect").val(0); 
Tushar Gupta
  • 15,504
  • 1
  • 29
  • 47
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,556
  • 3
  • 20
  • 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
  • 730
  • 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