I am trying to fire JQuery when I checkbox is checked. At first I realized my JQuery only works for static elements. I read through a couple posts and found out that I need .on("click, function()) in order to fire that same piece of javascript for dynamically added elements.
However, this method still doesn't work for me. Can anyone help? Thank you.
$(document).ready(function(){
$("input[name='todo']").on('click', function(){
var isChecked = this.checked
if (isChecked == true){
$(this).next().remove();
$(this).remove();
}
if (isChecked == false)
{
alert("checkbox is NOT checked");
}
});
});
My example app: http://jsfiddle.net/JSFoo/7sK7T/8/
.on()also has a second parameter for a target element selector. In the case of dynamically added elements, the primary selector should be a common static (non-changing) parent element (anywhere up to'body'ordocumentif necessary), and the second argument to.on()should be the selector for the dynamic target element..on()can get it to work, but don't really understand why it does what it does. Just hoping to help the OP a little more..on("click, function())...However, this method still doesn't work" - Did you think of reading the actual jQuery.on()documentation?