简体   繁体   中英

How can I change the text in element with jQuery?

I have this code (only the pertinent part):

 <script> $(function() { $('ul li').click(function() { $(this).addClass('picked').siblings().removeClass('picked'); }); }); $('picked').click(function() { var $this = $(this); $this.toggleClass('picked'); if ($this.hasClass('picked > rightPill-right')) { this.textContent = this.textContent.replace('Pick This One!', 'Got It!') }; }); </script> 
 <div class="timePill "> <div class="timePill-left "> <p>4:30 PM - 6:30 PM</p> </div> <div class="timePill-right"> <input type="radio" autocomplete="off" value="430"> <p>Pick This One!</p> </div> </div> 

My idea is that when someone selects the element, which is a radio button, instead of "Pick this one" it should read "Got It" . However, I can't make it work. What am I doing wrong?

You can do it with just CSS using checked and next sibling selector

 input[type="radio"] + label + label { display: none } input[type="radio"]:checked + label { display: none } input[type="radio"]:checked + label + label { display: inline } 
 <div class="timePill "> <div class="timePill-left "> <p>4:30 PM - 6:30 PM</p> </div> <div class="timePill-right"> <input type="radio" autocomplete="off" value="430" id="r1" name="foo"> <label for="r1">Pick This One!</label> <label for="r1">Got It!</label> </div> </div> <div class="timePill "> <div class="timePill-left "> <p>4:30 PM - 6:30 PM</p> </div> <div class="timePill-right"> <input type="radio" autocomplete="off" value="430" id="r2" name="foo"> <label for="r2">Pick This One!</label> <label for="r2">Got It!</label> </div> </div> 

Your code and your HTML don't seem to match into one example, but in any case you can do something like:

$('input').click(function () {
  $(this).parent().find('p').text('...');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM