Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting the text of an <option> element using jQuery

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.

like image 903
Toby Avatar asked Mar 21 '11 16:03

Toby


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.

Is text () a jQuery method?

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.


1 Answers

$('#select option[value="someValue"]').text("newText")

could use .html instead of .text, if adding html content.

like image 82
Anurag Avatar answered Oct 14 '22 18:10

Anurag