MaxwellLynn MaxwellLynn - 4 months ago 30
jQuery Question

Click event not firing on checkbox

Am I going crazy?

I can't get a click event to fire on a checkbox.

This is my JS:

$(document).ready(function() {
$('.checkbox').on('click', function () {
console.log('ello');
});
});


This is my php/html:

<ul class="category-filters">
<li>
<input class="checkbox" value="Customer Service" type="checkbox">Customer Service
</li>
<li>
<input class="checkbox" value="HR" type="checkbox">HR</li>
</ul>


It is worth pointing out that if I change the function/method to hide then it works...

but it does not trigger a click event. I've used on and click to try this..

Is it because the class selector I'm using returns an array?

Cheer

Answer

One way to do it is to use the change event.

$('input').on('change', function() {
  alert('ello')
})

jsbin: http://jsbin.com/xonecajeva/edit?html,css,js,output