Get the value of checked checkbox? Get the value of checked checkbox? javascript javascript

Get the value of checked checkbox?


None of the above worked for me but simply use this:

document.querySelector('.messageCheckbox').checked;

Happy coding.


For modern browsers:

var checkedValue = document.querySelector('.messageCheckbox:checked').value;

By using jQuery:

var checkedValue = $('.messageCheckbox:checked').val();

Pure javascript without jQuery:

var checkedValue = null; var inputElements = document.getElementsByClassName('messageCheckbox');for(var i=0; inputElements[i]; ++i){      if(inputElements[i].checked){           checkedValue = inputElements[i].value;           break;      }}


I am using this in my code.Try this

var x=$("#checkbox").is(":checked");

If the checkbox is checked x will be true otherwise it will be false.