How do i check to see if the checkbox is checked? [duplicate] - javascript

This question already has answers here:
How can I check if a checkbox is checked?
(18 answers)
Closed 2 years ago.
I want to implement a dark mode in my website. I have a switch with a checkbox input and I want to run a javascript function when it is checked so that the colors change. Here is the html code. I don't know if the CSS code is needed for this.
<label class="switch">
<input type="checkbox" id="darkmode">
<span class="slider round"></span>
</label>

var checkbox = document.getElementById("darkmode");
checkbox.addEventListener( 'change', function() {
if(this.checked) {
// Enable dark mode
} else {
// Disable dark mode
}
});

function check() {
var check= document.getElementById("darkmode").checked;
console.log(check);
}
<label class="switch">
<input type="checkbox" id="darkmode" >
<span class="slider round"></span>
<button onclick='check()'>press</button>
</label>

Related

How to set the checkbox value with JS? [duplicate]

This question already has answers here:
Check/Uncheck checkbox with JavaScript
(12 answers)
Closed 7 months ago.
I have parent element like:
<span style="background: yellow; padding: 50px;" onClick="setCheckBox()"><span>
<span> </span> <input type="checkbox" name="f01" value="100"></span></span>
and js function:
function setCheckBox() {
document.getElementsByTagName('input'[0].checked = 'true';
}
Want to change the checkbox input on parent click (yellow color)
Thanks in advance!
you would use .checked = true; like so
document.getElementsByTagName('input')[0].checked = true;
<input type="checkbox" name="f01" value="100">

jQuery not turning radio button off [duplicate]

This question already has answers here:
How to check/uncheck radio button on click?
(22 answers)
Closed 1 year ago.
I don't understand why my radio button will, un-check, then check, but won't turn back off again. Can someone please help explain this? Here's my HTML:
$('#member').click(() => {
if ($('#member').attr('checked')) {
$('#member').removeAttr('checked')
$('#member').prop('checked', false)
} else {
/* $('#member').attr('checked') */
$('#member').prop('checked', true)
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="radio" id="member" name="member" value="member" checked>
<label for="member">Member Reported</label><br>
You just add the checked attribute back again like below
$('#member').attr('checked','checked');
$('#member').click(() => {
if($('#member').attr('checked')) {
$('#member').removeAttr('checked')
$('#member').prop('checked',false)
} else {
$('#member').attr('checked','checked');
$('#member').prop('checked',true);
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<input type="radio" id="member" name="member" value="member" checked>
<label for="member" >Member Reported</label><br>
It is bad idea to use single radio button. Radio buttons are meant to be used in groups, as defined by their sharing the same name attribute.
You can use checkbox for the purpose.
But if you insist on to know why your code works only for first time, the answer is:
After first click else part of your code always run because you never set $('#member').attr('checked','checked'); in else part.

How to make an unchecked checkbox value 0 and checked value of 1?

I'm making a simple registration form with a declaration asking if the user has read the terms and conditions but when I console log the value of the checkbox it doesn't change depending on if it's checked or not. How do I do this? I've seen other people ask the question but they're using JS libraries like jQuery which I'm not using so how do you differentiate the value from checked and unchecked just using basic JS and HTML
<div class="item">
<input type="checkbox" id="checkbox" value="1">
<label>I accept the <a id="termsLink" href="https://imgur.com/5lXi3Lc" target="_blank">Terms and Conditions</a></label>
</div>
This is the div containing the checkbox.
You can add an event handler onClick in order to achieve this:
function handleClick(cb) {
cb.value = cb.checked ? 1 : 0;
console.log(cb.value);
}
<div class="item">
<input type="checkbox" id="checkbox" value="1" onclick='handleClick(this);'>
<label>I accept the <a id="termsLink" href="https://imgur.com/5lXi3Lc" target="_blank">Terms and Conditions</a></label>
</div>
You can use the .checked method:
var checkBox = document.getElementById("checkbox");
if(checkbox.checked){
console.log("checked");
}
else{
console.log("unchecked");
}
You need to test the .checked property. To convert it to an integer, you can use the bitwise OR operator.
document.getElementById('checkbox').addEventListener('change', function(e){
let checked = this.checked;
console.log(checked);
console.log(checked | 0);
});
<div class="item">
<input type="checkbox" id="checkbox">
<label>I accept the <a id="termsLink" href="https://imgur.com/5lXi3Lc" target="_blank">Terms and Conditions</a></label>
</div>

Is it possible to solve this situation with JavaScript/jQuery? [duplicate]

This question already has answers here:
How to allow only one radio button to be checked?
(8 answers)
Closed 3 years ago.
I have a code that checks if radio buttons are checked and if they are it set background color. It's all working fine, my problem is when I want to select another radio button, result is that all my radio buttons are selected on click but it needs to be just one.
$("input[type='radio']").each(function () {
if ($(this).is(":checked")) {
$(this).css("background", "yellow");
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="radio">
<input type="radio" checked>
You can test this out. Just try to select another radio button, you will see that radio buttons are selected (2 radio buttons).
What I want to achieve is that when you click on another radio button it needs to remove this checked class or any other idea. I can't switch between radio buttons.
Give them the same name:
$("input[type='radio']").each(function() {
if ($(this).is(":checked")) {
$(this).css("background", "yellow");
}
});
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<input type="radio" name="radio">
<input type="radio" name="radio" checked>
Radio button can be easily handle with name attribute below code may help you
<input type="radio" name="color">
<input type="radio" name="color" checked>
$("input[name='color']").each(function () {
if ($(this).is(":checked")) {
$(this).css("background", "yellow");
}
});
<input type="radio" name="sameName">
<input type="radio" name="sameName">
set their name like this way
To achieve the desired behavior with visible background color for each radio button, you could wrap each radio <input /> with a span element which would achieve the visual effect of a background color for the radio buttons:
/* Reusable selection of radio buttons */
const radioButtons = $("input[type='radio']");
/* Updates selected radio button parent backgrounds based on checked state */
function updateBackground() {
radioButtons.each(function() {
$(this).parent().css('background', $(this).is(":checked") ? 'yellow' : '');
});
}
/* Attach click behavior to each causing the background colors to update */
radioButtons.click(updateBackground);
/* Initalise the backgrounds and */
updateBackground();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<!-- Wrap each radio button with a span, which will be used to show a colored background
when the radio button is checked -->
<span>
<input type="radio" name="radio-group-name" checked>
</span>
<span>
<input type="radio" name="radio-group-name">
</span>

CheckAll/UncheckAll checkbox with jQuery

I have made a check-box checkall/uncheckall.
HTML
<div> Using Check all function </div>
<div id="selectCheckBox">
<input type="checkbox" class="all" onchange="checkAll('selectCheckBox','all','check','true');" />Select All
<input type="checkbox" class="check" onchange="checkAll('selectCheckBox','all','check','false');" />Check Box 1
<input type="checkbox" class="check" onchange="checkAll('selectCheckBox','all','check','false');" />Check Box 2
<input type="checkbox" class="check" onchange="checkAll('selectCheckBox','all','check','false');" />Check Box 3
<input type="checkbox" class="check" onchange="checkAll('selectCheckBox','all','check','false');" />Check Box 4
</div>
main.js
function checkAll(parentId,allClass,checkboxClass,allChecked){
checkboxAll = $('#'+parentId+' .'+allClass);
otherCheckBox = $('#'+parentId+' .'+checkboxClass);
checkedCheckBox = otherCheckBox.filter($('input[type=checkbox]:checked'));
if(allChecked=='false'){
if(otherCheckBox.size()==checkedCheckBox.size()){
checkboxAll.attr('checked',true);
}else{
checkboxAll.attr('checked',false);
}
}else{
if(checkboxAll.attr('checked')){
otherCheckBox.attr('checked',true);
}else{
otherCheckBox.attr('checked',false);
}
}
}
It works fine. But get bulky when I have whole lot of checkboxes. I want to do same work by using jQuery rather than putting onchange on each checkbox. I tried different sort of things but couldnot work. I tried following one:
$('.check input[type="checkbox"]').change(function(e){
checkAll('selectCheckBox','all','check','true');
});
to do same work as onchange event but didnot work. Where do I went wrong.
I think you just need this: You do not need to pass all the arguments and have the inline onchange event attached to it. You can simplify your code.
$(function () {
$('input[type="checkbox"]').change(function (e) {
if(this.className == 'all')
{
$('.check').prop('checked', this.checked); //Toggle all checkboxes based on `.all` check box check status
}
else
{
$('.all').prop('checked', $('.check:checked').length == $('.check').length); // toggle all check box based on whether all others are checked or not.
}
});
});
Demo
Your selector is wrong:
.check input[type="checkbox"]
Above selects any input of type checkbox that has the ancestor with class .check. It'll match this:
<div class="check">
<input type="checkbox".../>
</div>
it should be:
input.check[type="checkbox"]
You closed the string here $('.check input[type='checkbox']') instead, you should use double quotes $('.check input[type="checkbox"]')

Categories