Change the Text of a Option with jQuery

核能气质少年 提交于 2019-11-30 17:30:35

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');
$("#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/

$('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/

Change text of the selected option:

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

$('select option:contains("Newest")').addClass('changeToOldest');
$('select option:contains("Oldest")').addClass('changeToNewest');
$('.changeToOldest').text('Oldest').removeClass('changeToOldest');
$('.changeToNewest').text('Newest').removeClass('changeToNewest');
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!