$("button").click(function() { if ($("#showHideButton").val() == "Read More2")
$("#showHideButton").val(function(){
return "Read Less"
});});
I'm trying to get a button to toggle values.
$("button").click(function() { if ($("#showHideButton").val() == "Read More2")
$("#showHideButton").val(function(){
return "Read Less"
});});
I'm trying to get a button to toggle values.
The code is self explanatory.
Working example:
$("button").click(function () {
if ($("#showHideButton").text() == "Read More")
$("#showHideButton").text("Read Less");
else
$("#showHideButton").text("Read More");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="showHideButton">Read More</button>
You could do something like this:
$("button").on('click', function() {
if($(this).text() == 'Read More') {
$(this).text('Read Less');
} else {
$(this).text('Read More');
}
});
Arguably, switch/case
is performs better than if/else
, so if you are crazy worried about overhead, you could do that. I wouldn't worry about it, though, since it's something so simple. You probably couldn't even see a difference between the two.