Change the Text of a Option with jQuery

前端 未结 5 1966
难免孤独
难免孤独 2020-12-31 01:00

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

I\'d like to change Newest with Oldest in an option is a

提交评论

  • 2020-12-31 01:45
    $('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/

    0 讨论(0)
  • 2020-12-31 01:54

    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');
    
    0 讨论(0)
  • 2020-12-31 01:59
    $('select option:contains("Newest")').addClass('changeToOldest');
    $('select option:contains("Oldest")').addClass('changeToNewest');
    $('.changeToOldest').text('Oldest').removeClass('changeToOldest');
    $('.changeToNewest').text('Newest').removeClass('changeToNewest');
    
    0 讨论(0)
  • 2020-12-31 02:05
    $("#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/

    0 讨论(0)
  • 提交回复
    热议问题