2

I trying to change the text link with particular word with a click handler. When I click again the text link should change back. How can I do this?

<div class="checkLevel" id="damonkEYkEY">
    <span data-bind="css: safeLevelClass"> </span>
    <a href="#" id="checkLevelBtn">签到</a>
</div>
$(document).ready(function(){
    $("#damonkEYkEY").click(function(){
        $(".checkLevel a").text('签到成功');            
    });
});
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Morgan Ng
  • 813
  • 2
  • 10
  • 22
  • @guradio Hi , do you mind show me the way of the jquery how to do this ? im new on this . thanks you – Morgan Ng Oct 03 '16 at 08:21
  • 1
    This may be a duplicate question: http://stackoverflow.com/questions/13652835/button-text-toggle-in-jquery – JDW Oct 03 '16 at 08:25

3 Answers3

5

To achieve this you can provide a function to the text() method and use a ternary expression to read the current text value and update it accordingly. Try this:

$(document).ready(function() {
  $("#damonkEYkEY").click(function() {
    $(".checkLevel a").text(function(i, t) {
      return t == '签到' ? '签到成功' : '签到';
    });
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="checkLevel" id="damonkEYkEY">
  <span data-bind="css: safeLevelClass"> </span>
  <a href="#" id="checkLevelBtn">签到</a> 
</div>
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
0

Maybe like this:

$(document).ready(function(){
    var oldText = $(".checklevel a").text();
    $("#damonkEYkEY").click(function(){
        $(".checkLevel a").text('签到成功');

    });
    //Suppose back is an id
    $("#back").click(function(){
        $(".checkLevel a").text('oldText);
    });

});
John Kananakis
  • 133
  • 1
  • 9
0

Do you mean like this?

$(document).ready(function(){
    $("#damonkEYkEY").click(function(){
        var ttext = $('a#checkLevelBtn').text();          
        $("a#checkLevelBtn").text(ttext == '签到成功' ? '签到' : '签到成功');
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="checkLevel" id="damonkEYkEY">
    <span data-bind="css: safeLevelClass"> </span>
    <a href="#" id="checkLevelBtn">签到</a>
</div>
Fadhly Permata
  • 1,686
  • 13
  • 26