Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery Update text in a select dropdown list

Tags:

jquery

I am trying to update the text of one of the options on a select dropdown after an action on my page. Does anyone know how to do this in jquery? I have tried this:

$("#selectid").text("newtext"); 

But that will remove all of the other options in the select list and it makes it blank. I know this is not the right approach because I just want to update one of the option values. Thanks for the help

like image 979
Jquery_quest Avatar asked Jan 11 '10 17:01

Jquery_quest


People also ask

How do I get the text value of a selected option jQuery?

We can select text or we can also find the position of a text in a drop down list using option:selected attribute or by using val() method in jQuery. By using val() method : The val() method is an inbuilt method in jQuery which is used to return or set the value of attributes for the selected elements.

How do I get selected text option in select?

Use the selectedIndex Property We can get the index of the selected item with the selectedIndex property. Then we can use the text property to get the text of the selected item. We create the getSelectedText function that takes an element as the parameter. Then we check if selectedIndex is -1.


1 Answers

$('#selectid option:eq(NUMERIC_INDEX_GOES_HERE)').text('newtext');

or

$('#selectid').find('option[value="OPTION_VALUE"]').text('newtext');

or

$('#selectid option').filter('[value="OPTION_VALUE"]').text('newtext');

or

$('#selectid option:contains("OLD_TEXT_VALUE")').text('newtext');
like image 55
Corey Ballou Avatar answered Oct 05 '22 01:10

Corey Ballou