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
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.
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.
$('#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');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With