Reputation: 1337
I am trying to check if the box is checked or not with jquery. It doesn't work but I don't get why.
If I refresh the page and keep the checkbox check, then it will echo alert.
if($("#faith").is(':checked')){
alert('hello');
}
if($("#diet").is(':checked')){
alert('hello');
}
<input type="checkbox" name="cureway" id="faith" value="faith" /><label for="faith">My faith</label>
<input type="checkbox" name="cureway" id="diet" value="diet" /><label for="diet">My diet</label>
<input type="checkbox" name="cureway" id="exer" value="exer" /><label for="exer">My excercise</label>
Upvotes: 0
Views: 90
Reputation: 2480
Try with this code
$(function(){
$('input').on('click', function(){
if($("#faith").is(':checked')){
alert('hello');
}
if($("#diet").is(':checked')){
alert('hello');
}
})
})
Upvotes: 1
Reputation: 6828
Try this,
Javascript
$(document).ready(function(){
$(".checker").change(function() {
if ($("#faith").is(':checked')) {
alert('hello');
}
if ($("#diet").is(':checked')) {
alert('hello again');
}
if ($("#exer").is(':checked')) {
alert('hello again too');
}
});
});
Html
<input type="checkbox" name="cureway" id="faith" value="faith" class="checker" />
<label for="faith">My faith</label>
<input type="checkbox" name="cureway" id="diet" value="diet" class="checker" />
<label for="diet">My diet</label>
<input type="checkbox" name="cureway" id="exer" value="exer" class="checker" />
<label for="exer">My excercise</label>
Added a class to the checkbox.
https://jsfiddle.net/17m8wkwf/
Upvotes: 0
Reputation: 1423
$('input[type="checkbox"]').on('change', function() {
if($("#faith").is(':checked')){
alert('faith checked');
}
if($("#diet").is(':checked')){
alert('diet checked');
}
});
Hope this helps
Upvotes: 1
Reputation: 570
I think you are looking for this
$('input[type="checkbox"]').on('change', function() {
if($("#faith").is(':checked')){
alert('hello');
}
if($("#diet").is(':checked')){
alert('hello');
}
});
Here is the fiddle.
https://jsfiddle.net/rrehan/sfo45mpb/1/
Upvotes: 1