I have a checkbox with a button, i want when user check the check box and click on the button show an alert and when checkbox is unchecked and user clicks on the button do something else.
I did something but i need help to acplish it. Any help greatly appreciated
$(document).ready(function(e) {
$('#remember').click(function() {
if (this.checked) {
alert("This is checked and you cliked");
} else {
alert("This is unchecked and you cliked");
}
});
});
JSFIDDLE
I have a checkbox with a button, i want when user check the check box and click on the button show an alert and when checkbox is unchecked and user clicks on the button do something else.
I did something but i need help to acplish it. Any help greatly appreciated
$(document).ready(function(e) {
$('#remember').click(function() {
if (this.checked) {
alert("This is checked and you cliked");
} else {
alert("This is unchecked and you cliked");
}
});
});
JSFIDDLE
Share Improve this question asked Sep 19, 2014 at 4:47 AmirAmir 5372 gold badges8 silver badges25 bronze badges 1- 1 try replacing (this.checked) with ($('input[type="checkbox"]').is(':checked')). – Harish Ambady Commented Sep 19, 2014 at 4:55
4 Answers
Reset to default 4You are showing alerts in Checkbox's onclick. You would need to add the code in button's onClick.
See updated fiddle
$(document).ready(function(e) {
$('#buttonId').click(function() {
if ($('#remember').is(':checked')) {
alert("This is checked and you cliked");
} else {
alert("This is unchecked and you cliked");
}
});
});
Hope this helps.
You can use jquery .is() selector for this:-
$('input[type="button"]').click(function() {
if ($('#remember').is(':checked')) {
alert("This is checked and you cliked");
} else {
alert("This is unchecked and you cliked");
}
});
You can set id or class to button
$(document).ready(function(e) {
$('#btnEnter').click(function() {
if ($('#remember').is(':checked')) {
alert("This is checked and you cliked");
} else {
alert("This is unchecked and you cliked");
}
});
});
Demo
use Prop();
$('[type=button]').click(function () {
if ($("#remember").prop("checked")) {
alert("This is checked and you cliked");
} else {
alert("This is unchecked and you cliked");
}
});
DEMO
$(document).ready(function(e) {
$('#buttonId').click(function() {
if ($('#remember').prop('checked')) {
alert("This is checked and you cliked");
} else {
alert("This is unchecked and you cliked");
}
});
});