I have a div with a p inside it that says 'Fold it!'. When I click the div, the p's text changes to 'Expand it'. How can I make so when I click the div for the second time it will change back to 'Fold it'?
HTML:
<div id="fold">
<p id="fold_p">Fold it</p>
</div>
JQuery:
<script>
$(document).ready(function () {
$("#fold").click(function () {
$("#fold_p").text("Expand it");
} )
} );
</script>
Also, is it possible to make the transition a fade? Any help is much appreciated :)
Answer: Use the jQuery prop() and html() Methods You can simply use the jQuery prop() method to change the text of the buttons built using the HTML <input> element, whereas to change the text of the buttons which are created using the <button> element you can use the html() method.
So onclick creates an attribute within the binded HTML tag, using a string which is linked to a function. Whereas . click binds the function itself to the property element.
$(document).ready(function () {
$("#fold").click(function () {
$("#fold_p").fadeOut(function () {
$("#fold_p").text(($("#fold_p").text() == 'Fold it') ? 'Expand it' : 'Fold it').fadeIn();
})
})
});
jsFiddle example
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With