Using jQuery what is the best way of editing option text for a given select value.
I know the value of the option I want to edit. I thought it would be something like...
$('#select').val().$('option').html('New Text');
But I am clearly missing something.
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.
jQuery text() MethodThe text() method sets or returns the text content of the selected elements. When this method is used to return content, it returns the text content of all matched elements (HTML markup will be removed). When this method is used to set content, it overwrites the content of ALL matched elements.
$('#select option[value="someValue"]').text("newText")
could use .html
instead of .text
, if adding html content.
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