Setting the text of an <option> element using jQuery

Using jQuery is the best way to edit option text for a given selection value.

I know the value of the parameter that I want to change. I thought it would be something like ...

$('#select').val().$('option').html('New Text'); 

But I obviously missed something.

+8
javascript jquery html select
source share
4 answers
 $('#select option[value="someValue"]').text("newText") 

can use .html instead of .text if you add html content.

+14
source share

Something like that?

 $('#select').children('option').text('New Text'); 

See Selectors for more information on choosing the right item. You could do something like

 $('#select').children('option:first').text('New Text'); 

Or you can use

 $('#select').children('option[value="thevalue"]').text('New Text'); 

If you know the meaning.

+6
source share

You are probably looking for : selected selector and text () :

 $("#select option:selected").text("New Text"); 
+4
source share

This will help you get the value of the parameter,

 $('#select_id').children('option[value="thevalue"]').text('New Text'); 

And you can set the selected default value using the prop() function. An example is here .

+2
source share

All Articles