zero <...">

How to set the first value of a Select Box parameter using jQuery?

I have a selection list:

<select id='myList'> <option value="">zero</option> <option value="1">one</option> <option value="2">two</option> <option value="3">three</option> </select> 

How to set value of parameter value to 0 using jQuery?

I tried $("#myList > option").attr("value", "0");

But that changed them all,

I also tried $("#myList:first-child").attr("value", "0"); but this violates the selection box.

Any ideas?

Thanks, Cohan.

+4
source share
2 answers
 $("#myList > option:first").attr("value", 0); 

use : first to filter

you also can,

 $("#myList :first-child").attr("value", 0); // watch out for the space in the selector 
+7
source

Try the following:

 $("#myList option[value='']").attr("value", "0"); 
+2
source

Source: https://habr.com/ru/post/1314412/


All Articles