开发者

jquery toggle text only changes once

开发者 https://www.devze.com 2023-01-28 12:17 出处:网络
I have the following code which should change the value of an h2 from \"Show More\" to \"Show Less\" and then back again when the parent is clicked.

I have the following code which should change the value of an h2 from "Show More" to "Show Less" and then back again when the parent is clicked.

The if() tests ok and changes the text to Show Less but it will never change it back again and always displays the alert "Show More" so it just isn't detecting the else() condition.

I have searched high and low to find out why but haven't found a solution yet.

I know this a noob question so apologise for this but I have tried finding the solution before posting here.

Thanks.

开发者_如何学运维
$('#panels > .feature').click(function() {
    if($(this).children('h2').val('Show More')) {
        $(this).children('h2').text('Show Less');
        alert('Show More was found');     
    }
    else {
        $(this).children('h2').text('Show More');
        alert('Show Less was found');
    }   
      $(this).next(".info_box").animate(
     {'height':'toggle'}, 'slow', 'linear'
     );    
});

The HTML in question is:

<div id="panels">
 <div id="Email" class="feature">
    <h1>LiveContent</h1><h2>Show More</h2>
 </div>
<div class="info_box">Information will go in this div which should slide in and out.</div>
<div id="Email" class="feature">
  <h1>Publishing Solutions</h1><h2>Show More</h2>
 </div>
<div class="info_box">Information will go in this div which should slide in and out.</div> 
<div id="Email" class="feature">
  <h1>Title 1</h1><h2>Show More</h2>
 </div>
<div class="info_box">Information will go in this div which should slide in and out.</div>
</div>


if($(this).children('h2').val('Show More')) {

Should this be .text instead of .val?


You're using .val() to check the text, use .text() instead (and compare the result, don't set it), like this:

if($(this).children('h2').text() == 'Show More')) { //here
    $(this).children('h2').text('Show Less');
    alert('Show More was found');     
}
else {
    $(this).children('h2').text('Show More');
    alert('Show Less was found');
}

Also, you can slim it down overall by using a function for .text() and .slideToggle(), like this:

$('#panels > .feature').click(function() {
    $(this).children('h2').text(function(i, t) {
      return t == 'Show More' ? 'Show Less' : 'Show More';
    });
    $(this).next(".info_box").slideToggle("slow");
});

You can test it out here.


The function .val('Show More) tries to set the HTML attribute value to 'Show More'. If you called it without parameters, it would act as a getter, not a setter - which is what you want. But for the text, not the value.

So instead of .val('Show More'), you want something like .text() == 'Show More'.


You are setting the text instead of comparing it. Use the text method without a parameter to get the value, and then compare it:

if($(this).children('h2').text() == 'Show More')

The code didn't work correctly even the first time, as it always did the same thing. The reason that it seemed to work the first time is that it happened to always do what it was supposed to do the first time.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号