vendredi 23 octobre 2015

Can I access this checkbox using this?

In my HTML file, I have the following list:

<ul >
       <li > <input type="checkbox" name="cbox" >do assignment 3</li>
        <li> <input type="checkbox" name="cbox" >study for test</li>
        <li > <input type="checkbox" name="cbox" >buy groceries</li>
        <li ><input type="checkbox" name="cbox">do more work</li> 
      </ul>

and I have the following function in my javascript file:

     $list.on('click', 'li', function() {
      var $this = $(this);
      ..some code
 }

I honestly don't feel like the rest of the code in the js file is very important, so I'll just ask what I'm confused about.

  1. I notice that anytime I check on the checkbox, it also counts it as me having clicked on that list item, and so that function is called. Is there anyway to separate this so that checking or unchecking a checkbox does not automatically mean the same as clicking on the list item? I would think there probably isn't, because there the checkbox code is surrounded by <li> </li> tags. Does this mean that..it somehow becomes a part (or a child? Not sure about the terminology) of the li element? And if so, does that mean that I should be able to access the value of the checkbox using $this?

I tried doing $this.is(':checked') but that always seems to return false.




Aucun commentaire:

Enregistrer un commentaire