15

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.

1
  • 1
    $('#select option:selected').text("new text") Commented Mar 21, 2011 at 16:42

4 Answers 4

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

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

Sign up to request clarification or add additional context in comments.

Comments

8

Something like this?

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

Have a look at Selectors for more information on selecting the correct 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 value.

1 Comment

none of them works for me and you have an error in .children. there is no dot after children
5

You're probably looking for the :selected selector and the text() method:

$("#select option:selected").text("New Text");

Comments

3

This one help you to get the options value,

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

And you can set default selected value by using prop() function. Take an example here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.