I have some jQuery checkbox buttons, and they work fine. However, I would like to change their text upon a click. for example: the button’s text is “click me”. when the user clicks it, i needs to change to “thanks for clicking”, for example.
This is what I am trying using:
<script>
$(function() {
$("#button").button();
$("#button").click(function(){
if($("#label").is(':checked')) {
$("#label span").text("Hide");
}
else {
$("#label span").text("Show");
}
});
});
</script>
<input id='button' type='checkbox' />
<label id='label' for="button">Show/Hide</label>
This is your first problem:
<label>elements don’t get “checked” only their checkboxes do. Change it to:In the code above,
thisrefers to the checkbox element that has been clicked, and we’re looking to see if thecheckedproperty contains the valuetrue. It’s much more efficient that.is(':checked').Also, the
<label>element has no<span>child, it just has text, soshould be
But you could shorten the whole thing using the ternary conditional operator:
Working demo: http://jsfiddle.net/AndyE/qnrVp/