jQuery or js check if checkbox is checked [duplicate]
up vote
-1
down vote
favorite
This question already has an answer here:
jQuery checkbox change and click event
16 answers
Im trying to make checkbox fire an alert if it is checked.
This is my code below. What am I doing wrong here?
var $isgdpr = $("#isgdpr").is(':checked');
if ($isgdpr) {
alert('Checked!');
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label class="switch">
<input type="checkbox" id="isgdpr" checked="checked">
</label>
javascript jquery html
marked as duplicate by Rory McCrossan
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 8 at 9:13
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
up vote
-1
down vote
favorite
This question already has an answer here:
jQuery checkbox change and click event
16 answers
Im trying to make checkbox fire an alert if it is checked.
This is my code below. What am I doing wrong here?
var $isgdpr = $("#isgdpr").is(':checked');
if ($isgdpr) {
alert('Checked!');
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label class="switch">
<input type="checkbox" id="isgdpr" checked="checked">
</label>
javascript jquery html
marked as duplicate by Rory McCrossan
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 8 at 9:13
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
I placed your code in a snippet, where it works absolutely fine. If you're expecting this to work when the checkbox is changed, place your code in achange
event handler.
– Rory McCrossan
Nov 8 at 9:13
Its not popping up alert box when i check the checkbox. Not even your corrected code for me (I ran the snippet).
– Wed
Nov 8 at 9:15
As I stated above, it only runs on load. If you want the logic to runs when the checkbox is changed you need to wrap your code in a change event handler. See the duplicate for more information
– Rory McCrossan
Nov 8 at 9:18
I have expanded this question. Should I open a new thread?
– Wed
Nov 8 at 9:24
The question hasn't changed. Unless you're intending to ask about a completely different topic you don't need to open anything. Have you read the duplicate I marked yet? Or even the answer below? Both solve your problem.
– Rory McCrossan
Nov 8 at 9:25
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
This question already has an answer here:
jQuery checkbox change and click event
16 answers
Im trying to make checkbox fire an alert if it is checked.
This is my code below. What am I doing wrong here?
var $isgdpr = $("#isgdpr").is(':checked');
if ($isgdpr) {
alert('Checked!');
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label class="switch">
<input type="checkbox" id="isgdpr" checked="checked">
</label>
javascript jquery html
This question already has an answer here:
jQuery checkbox change and click event
16 answers
Im trying to make checkbox fire an alert if it is checked.
This is my code below. What am I doing wrong here?
var $isgdpr = $("#isgdpr").is(':checked');
if ($isgdpr) {
alert('Checked!');
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label class="switch">
<input type="checkbox" id="isgdpr" checked="checked">
</label>
This question already has an answer here:
jQuery checkbox change and click event
16 answers
var $isgdpr = $("#isgdpr").is(':checked');
if ($isgdpr) {
alert('Checked!');
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label class="switch">
<input type="checkbox" id="isgdpr" checked="checked">
</label>
var $isgdpr = $("#isgdpr").is(':checked');
if ($isgdpr) {
alert('Checked!');
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label class="switch">
<input type="checkbox" id="isgdpr" checked="checked">
</label>
javascript jquery html
javascript jquery html
edited Nov 8 at 9:12
Rory McCrossan
239k29203244
239k29203244
asked Nov 8 at 9:09
Wed
8610
8610
marked as duplicate by Rory McCrossan
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 8 at 9:13
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Rory McCrossan
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 8 at 9:13
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
I placed your code in a snippet, where it works absolutely fine. If you're expecting this to work when the checkbox is changed, place your code in achange
event handler.
– Rory McCrossan
Nov 8 at 9:13
Its not popping up alert box when i check the checkbox. Not even your corrected code for me (I ran the snippet).
– Wed
Nov 8 at 9:15
As I stated above, it only runs on load. If you want the logic to runs when the checkbox is changed you need to wrap your code in a change event handler. See the duplicate for more information
– Rory McCrossan
Nov 8 at 9:18
I have expanded this question. Should I open a new thread?
– Wed
Nov 8 at 9:24
The question hasn't changed. Unless you're intending to ask about a completely different topic you don't need to open anything. Have you read the duplicate I marked yet? Or even the answer below? Both solve your problem.
– Rory McCrossan
Nov 8 at 9:25
add a comment |
I placed your code in a snippet, where it works absolutely fine. If you're expecting this to work when the checkbox is changed, place your code in achange
event handler.
– Rory McCrossan
Nov 8 at 9:13
Its not popping up alert box when i check the checkbox. Not even your corrected code for me (I ran the snippet).
– Wed
Nov 8 at 9:15
As I stated above, it only runs on load. If you want the logic to runs when the checkbox is changed you need to wrap your code in a change event handler. See the duplicate for more information
– Rory McCrossan
Nov 8 at 9:18
I have expanded this question. Should I open a new thread?
– Wed
Nov 8 at 9:24
The question hasn't changed. Unless you're intending to ask about a completely different topic you don't need to open anything. Have you read the duplicate I marked yet? Or even the answer below? Both solve your problem.
– Rory McCrossan
Nov 8 at 9:25
I placed your code in a snippet, where it works absolutely fine. If you're expecting this to work when the checkbox is changed, place your code in a
change
event handler.– Rory McCrossan
Nov 8 at 9:13
I placed your code in a snippet, where it works absolutely fine. If you're expecting this to work when the checkbox is changed, place your code in a
change
event handler.– Rory McCrossan
Nov 8 at 9:13
Its not popping up alert box when i check the checkbox. Not even your corrected code for me (I ran the snippet).
– Wed
Nov 8 at 9:15
Its not popping up alert box when i check the checkbox. Not even your corrected code for me (I ran the snippet).
– Wed
Nov 8 at 9:15
As I stated above, it only runs on load. If you want the logic to runs when the checkbox is changed you need to wrap your code in a change event handler. See the duplicate for more information
– Rory McCrossan
Nov 8 at 9:18
As I stated above, it only runs on load. If you want the logic to runs when the checkbox is changed you need to wrap your code in a change event handler. See the duplicate for more information
– Rory McCrossan
Nov 8 at 9:18
I have expanded this question. Should I open a new thread?
– Wed
Nov 8 at 9:24
I have expanded this question. Should I open a new thread?
– Wed
Nov 8 at 9:24
The question hasn't changed. Unless you're intending to ask about a completely different topic you don't need to open anything. Have you read the duplicate I marked yet? Or even the answer below? Both solve your problem.
– Rory McCrossan
Nov 8 at 9:25
The question hasn't changed. Unless you're intending to ask about a completely different topic you don't need to open anything. Have you read the duplicate I marked yet? Or even the answer below? Both solve your problem.
– Rory McCrossan
Nov 8 at 9:25
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
Your code work on page load but if you want to run it on check/uncheck of checkbox, you should put it in onchange
event handler of checkbox
$("#isgdpr").change(function(){
if ($(this).is(':checked'))
alert('Checked!');
});
$("#isgdpr").change(function(){
if ($(this).is(':checked'))
console.log('Checked!');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label class="switch">
<input type="checkbox" id="isgdpr">
</label>
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Your code work on page load but if you want to run it on check/uncheck of checkbox, you should put it in onchange
event handler of checkbox
$("#isgdpr").change(function(){
if ($(this).is(':checked'))
alert('Checked!');
});
$("#isgdpr").change(function(){
if ($(this).is(':checked'))
console.log('Checked!');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label class="switch">
<input type="checkbox" id="isgdpr">
</label>
add a comment |
up vote
2
down vote
accepted
Your code work on page load but if you want to run it on check/uncheck of checkbox, you should put it in onchange
event handler of checkbox
$("#isgdpr").change(function(){
if ($(this).is(':checked'))
alert('Checked!');
});
$("#isgdpr").change(function(){
if ($(this).is(':checked'))
console.log('Checked!');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label class="switch">
<input type="checkbox" id="isgdpr">
</label>
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Your code work on page load but if you want to run it on check/uncheck of checkbox, you should put it in onchange
event handler of checkbox
$("#isgdpr").change(function(){
if ($(this).is(':checked'))
alert('Checked!');
});
$("#isgdpr").change(function(){
if ($(this).is(':checked'))
console.log('Checked!');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label class="switch">
<input type="checkbox" id="isgdpr">
</label>
Your code work on page load but if you want to run it on check/uncheck of checkbox, you should put it in onchange
event handler of checkbox
$("#isgdpr").change(function(){
if ($(this).is(':checked'))
alert('Checked!');
});
$("#isgdpr").change(function(){
if ($(this).is(':checked'))
console.log('Checked!');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label class="switch">
<input type="checkbox" id="isgdpr">
</label>
$("#isgdpr").change(function(){
if ($(this).is(':checked'))
console.log('Checked!');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label class="switch">
<input type="checkbox" id="isgdpr">
</label>
$("#isgdpr").change(function(){
if ($(this).is(':checked'))
console.log('Checked!');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label class="switch">
<input type="checkbox" id="isgdpr">
</label>
answered Nov 8 at 9:12
Mohammad
13.8k93158
13.8k93158
add a comment |
add a comment |
I placed your code in a snippet, where it works absolutely fine. If you're expecting this to work when the checkbox is changed, place your code in a
change
event handler.– Rory McCrossan
Nov 8 at 9:13
Its not popping up alert box when i check the checkbox. Not even your corrected code for me (I ran the snippet).
– Wed
Nov 8 at 9:15
As I stated above, it only runs on load. If you want the logic to runs when the checkbox is changed you need to wrap your code in a change event handler. See the duplicate for more information
– Rory McCrossan
Nov 8 at 9:18
I have expanded this question. Should I open a new thread?
– Wed
Nov 8 at 9:24
The question hasn't changed. Unless you're intending to ask about a completely different topic you don't need to open anything. Have you read the duplicate I marked yet? Or even the answer below? Both solve your problem.
– Rory McCrossan
Nov 8 at 9:25