how do I Check if a checkbox is checked using jquery or javascript?


Hello,I would like to get check if checkbox is checked or not on button click and Change the form action url according to it, how do it using javascript or jquery?

Right now using this code

if($('#ChangeURL').attr('checked')) {
   //some code
}

but not working as needed.

 


Asked by:- Sam
0
: 2343 At:- 1/18/2018 11:25:50 AM
javascript jQuery check if checkbox is checked javascript







1 Answers
profileImage Answered by:- manish

You should use .is(:checked) in jQuery code to check if checkbox is checked, so your code will be

if ($('#ChangeURL').is(':checked')) {
 //do something here
}

Using jQuery 1.6+

if ($('#ChangeURL').prop('checked')) {
    //Checked
}

OR using Javascript checked property of a checkbox

if(document.getElementById('ChangeURL').checked) {
    //Checked Code
} else {
    //Note checked
}

Here are the all possible jQuery method to check, Checkbox state

//here changeURL is Id of the element
$('#changeURL').is(":checked")

$('#changeURL').prop('checked')

$('#changeURL')[0].checked

$('#changeURL').get(0).checked
2
At:- 1/20/2018 8:18:29 AM
if ($('#ChangeURL').prop('checked')) { //Checked } is good options to check if checkbox is checked or not 0
By : Sam - at :- 2/2/2018 7:49:50 AM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use