Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

How can I get a checkbox's value in jQuery?

user-image
Question added by Zaid Rabab'a , Technical Team Lead , ESKADENIA Software
Date Posted: 2013/04/08
Hazem Salama
by Hazem Salama , Senior Member of Technical Staff , Verizon Communications

If you mean the value that you have in the value attribute of the checkbox then you can use this //Assuming your check box id is chkBox $('#chkBox').val(); If you mean the state, whether checked or not, you can use plain old javascript document.getElementById('chkBox').checked; // will return false or true depending on the state // OR in jquery $('#chkBox').is(':checked');

Muhammad Majid Saleem
by Muhammad Majid Saleem , Senior PHP Developer / Project Manager , SwaamTech

There are two parts of the question I guess.
They are: 1.
You want to get checkbox value regardless of whether it is checked or not? 2.
You want to get THE "Checked" checkbox value.
Answers: 1.
You can get checkbox value: $("#checkbox_id").val(); $(".checkbox_class").val(); // this is return in array if particular class is applied on many other fields or checkbox.
2.
You can get Checed checkbox value: if( $("#checkbox_id").attr("checked") ) { $("#checkbox_id").val(); }

Faizan Ahmad
by Faizan Ahmad , Software Engineer , cardekho.com

on check box change event $('#checkboxid').change(function(){ if($('#checkboxid').is('checked')) { var val = $('#checkboxid').val(); console.log(val); } });

Deleted user
by Deleted user

function checkboxValues() { var values = []; $('#checkbox :checked').each(function() { values.push($(this).val()); }); console.log(values); } don't forget to enable firebug to see the results .

Panneer selvam xavier
by Panneer selvam xavier , Supply Chain Analyst / Trainer , Infonet

$("input[type='checkbox']").val(); Or if you have set a class or id for it, you can: $('#check_id').val(); $('.check_class').val(); Also you can check whether it is checked or not like: if ($('#check_id').is(":checked")) { // it is checked }

Mohamed Sadek Kamal Mohamed
by Mohamed Sadek Kamal Mohamed , UI Developer , MSN Arabia

.is(':checked')

Deleted user
by Deleted user

$("#theChk").prop("checked") 

$("#theChk").attr("checked") 

$("#theChk").val()

Besim Dauti
by Besim Dauti , Ceo / Front End Developer , Nunforest

if ($('#check_id').is(":checked")) { // it is checked }

More Questions Like This