mercredi 7 août 2019

Checking if a checkbox is ticked using jQuery: different methods?

I need to check whether a textbox in a form on my webpage has been checked. I implement the textbox in my html code as such:

<input type="checkbox" id="language1" name="language1" 
value="Python"> Python

I tried using the following code to check if the checkbox had been ticked:

if($('#language1').checked){
    // do some stuff }

This did not work - the website never recognised the box being ticked. I then poked around Stack Overflow and found two alternatives to the above, namely:

if($('#language1')[0].checked){

and

if($('#language1').is(':checked'));

Each of the two alternatives worked, but my original implementation didn't. I don't understand why that is the case - I thought all three options should essentially do the same thing? Keen to get to the bottom of this so will appreciate an explanation.




Aucun commentaire:

Enregistrer un commentaire