23

How can I change the text of an option with jQuery?

I'd like to change Newest with Oldest in an option is a <select> element.

$('select option:contains("Newest")').changeText('Oldest');

.replaceWith would not work for me.

Thanks.

----Update----

If my script looks like this:

$('select option:contains("Newest")').text('Oldest');
$('select option:contains("Oldest")').text('Newest');

Then I'm canceling out the first script, however, that isn't what I want. I have two options one that reads "Newest" which I'd like to replace with "Oldest" and one that reads "Oldest" which I'd like to replace with "Newest". How can I accomplish that?

5
  • Does text() not work for you? Commented Feb 27, 2012 at 1:41
  • Your syntax is broken. Where did you find the .changeText method? Commented Feb 27, 2012 at 1:43
  • @amnotiam It was a placeholder to display what I was trying to accomplish Commented Feb 27, 2012 at 1:44
  • Use the method @XavierHolt pointed out, but fix your syntax by either removing the inner single quotes, changing them to double quotes, or escaping them. Commented Feb 27, 2012 at 1:45
  • @amnotiam Yes, I took care of that... Commented Feb 27, 2012 at 1:48

6 Answers 6

58

You should start a new question but here's the answer:

$('select option:contains("Newest")').text('TEMPEST');
$('select option:contains("Oldest")').text('Newest');
$('select option:contains("TEMPEST")').text('Oldest');
Sign up to request clarification or add additional context in comments.

1 Comment

This is an unneeded 3rd query of the dom. Once you create a jquery object, it maintains the list of elements that it matched at the time of creation. Simply save the jquery objects to a variable before modifying. No need to query an extra time.
5
$('select option:contains("Newest")').each(function(){
   var $this = $(this);

   $this.text($this.text().replace("Newest","Oldest"));    
});​

http://jsfiddle.net/eSa5p/

EDIT: Answer to the new question:

var $newest = $('select option:contains("Newest")');

$('select option:contains("Oldest")').text('Newest');
$newest.text('Oldest');

http://jsfiddle.net/eSa5p/3/

1 Comment

Unfortunately, this did not work for me, although it would've been preferred
3
$("#mySelect option").html(function(i,str){
  return str.replace(/Newest|Oldest/g,  // here give words to replace
     function(m,n){
         return (m == "Newest")?"Oldest":"Newest";  // here give replacement words
     });
});

demo : http://jsfiddle.net/diode/eSa5p/2/

Comments

3

Change text of the selected option:

$("#select").find("option:selected").text('Newest');

Comments

3

You can do that by a simple script

$("option[value=optionvalue]").html('New text');

Comments

0
$('select option:contains("Newest")').addClass('changeToOldest');
$('select option:contains("Oldest")').addClass('changeToNewest');
$('.changeToOldest').text('Oldest').removeClass('changeToOldest');
$('.changeToNewest').text('Newest').removeClass('changeToNewest');

1 Comment

You are querying the DOM 4 times when twice is all that is needed. Once you create a jquery object, it maintains the list of elements that it matched at the time of creation. Simply save the jquery objects to a variable before modifying. No need to query an extra 2 times, and add and remove classes unnecessarily .

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.