Now, I have to show you some easily understood examples for how to get checked and unchecked checkbox value in jquery. As well as, given source code using jquery and explain in detail get checkbox value different ways.
Also, we describe a few events relative to this topic. Afterward, get multiple checkboxes with the use of the jquery selector.
As an example, add and remove the checkbox value events from the array using jquery. Now, you can see the example in the below section.
How to Get Checked and Unchecked Checkbox Value in jQuery
Firstly, create an HTML structure and added a few checkboxes under the HTML form.
<!-- $('.checkbox').prop('checked', true); //false for uncheck
//jQuery 1.5.x and below use
$('.checkbox').attr('checked', true); //false for uncheck -->
<!DOCTYPE html>
<html>
<head>
<title>Checked and unchecked value</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<section>
<h1>My Selected Color</h1>
<form >
Red<input type="checkbox" class="red" name="" value="red"><br/>
Green<input type="checkbox" class="green" name="green" value="green"><br/>
Blue<input type="checkbox" class="blue" name="blue"><br/>
White<input type="checkbox" class="white" name="white"><br/>
<input type="button" id="submit" name="submit" value="submit">
</form>
<div class="showCheckedValue"></div>
</section>
<script type="text/javascript">
/********Button Click Event get multiple value********/
$('#submit').click(function(){
var redval = $('.red').val();
var grenval = $('.green').val();
$('.showCheckedValue').text(redval+'&'+grenval );
});
</script>
</body>
</html>
Above all, given a script get check value by click. So, I added classes to each input and also added a class in button.
After that, implement a click event using the button class name is ‘submit’. secondly, using select event and get check input value by click. You can see out in the below section.
Note:- focus on class and id name which you add is selected. As well, the main jquery tags use ‘this’ to get the value of checked and unchecked
Even, we created a simple form to display multiple checkbox fields. Afterward, we have to give an example in the below code go and follow this code.
<form >
Red<input type="checkbox" class="red" name="" value="red"><br/>
Green<input type="checkbox" class="green" name="green" value="green"><br/>
Blue<input type="checkbox" class="blue" name="blue"><br/>
White<input type="checkbox" class="white" name="white"><br/>
<input type="button" id="submit" name="submit" value="submit">
</form>
After that, you use this code to create an input form then write a functionally of jquery. As well, jquery set checkbox checked based on value. Accordingly, follow the below section and see the full jquery code.
$('#submit').click(function(){
var redval = $('.red').val();
var grenval = $('.green').val();
$('.showCheckedValue').text(redval+'&'+grenval );
});
in the final analysis, there is a click event when click the submit button. So, you can get the input value at a variable. Such as we have to use multiple variables to get checkbox check value from this method.
How to Check Checkbox is Checked or not in jquery on Button Click
Similarly, the above example and this point both are the same but these additions of conditions check and uncheck the checkbox. So, I am going to start with the code of the example. Which you can easily understand all points.
.
$(document).ready(function(){
$(".clickbtn").click(function(){
$("#checkbox1").prop("checked", true);
});
$(".clicbtn-unchecked").click(function(){
$("#checkbox1").prop("checked", false);
});
});
Now, we have provided a syntax of line. Whom you use the conditions to change checked and unchecked checkbox this condition prop().
$("#checkbox1").prop("checked", false);
Above, display a condition with jquery prop(). So, We can pass two parameters in this function like can see above syntax. If you want to check the value from the checkbox to mention true otherwise should be used false to change automatically checkbox checked and unchecked with the conditions.
$("#checkbox1").prop("checked", true);
Specifically, here is some relative important topic to follow and get code with the example.
- How to Get Radio Button Value in PHP Without Submit
- JQuery Radio Button Checked Event By Id & Class
- JavaScript Change Image onclick Event
Conclusion
finally, display all examples of How to Get Check and Unchecked Checkbox Value in jQuery. If you have any query and suggest with this post should ask anytime. Also, you can put your query in the comment section. I have to reply as shortly.
I every time spent my half an hour to read this website’s posts everyday along with a
mug of coffee.