ngplayground ngplayground - 1 year ago 95
jQuery Question

jQuery toggle and changing text


$('#message div').hide();
$('#message div').slideToggle();


<div id="message">
<p class="intro">This is an introduction.</p>
<p>This is the first paragraph thats orginally hidden</p>
<p>This is the second paragraph</p>
<a href="#" class="readmore">Read More</a>

When the read more button is clicked the content is slideToggled and shows to everyone and clicked again it hides to only show my .intro but I would like to toggle the Read More text between Read More and Show Less.

How is this possible whilst using slideToggle

Answer Source

Check the text of current element inside its click event and use condition, see if you have to change the text if yes, change it using text() again ;).

Try this:

    $('#message div').slideToggle();
    $(this).text( $(this).text() == 'Read More' ? "Show Less" : "Read More"); // using ternary operator.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download