Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why is my checkbox.change not working (jquery)?

I've done similar things before, click a checkbox then do something (.parent() etc) but for some reason it's not registering this time. Can anyone see why?

$.each( email_list, function( key, value ) {
$("#email_span").after("<tr><td><input type='checkbox' name='member' checked='checked' onclick='check_change(this);' /></td><td>" + value.name + "</td><td>" + value.email + "</td></tr>");
$("#emails").prepend(value.email + ";");
});

$('.member').change(function() {
   alert ("FECK");
   if(this.checked) {
   }
});

It is all within the $(document).ready block. I've got it working with a function call but was curious why the jQuery .change wasn't working?

like image 976
Dird Avatar asked Sep 08 '14 09:09

Dird


People also ask

How do you check if a check box is checked or not in jQuery?

prop() and is() method are the two way by which we can check whether a checkbox is checked in jQuery or not. prop(): This method provides an simple way to track down the status of checkboxes. It works well in every condition because every checkbox has checked property which specifies its checked or unchecked status.

How can I get checkbox value in jQuery?

With jQuery, you can use the . val() method to get the value of the Value attribute of the desired input checkbox.

Which syntax is used for a checkbox?

The <input type="checkbox"> defines a checkbox. The checkbox is shown as a square box that is ticked (checked) when activated. Checkboxes are used to let a user select one or more options of a limited number of choices.


1 Answers

If the element has the 'change' event bound and if you need to fire the 'change' event after changing the checkbox property you can use the alt workaround below:

If you want to check:

$('#MyCheckbox input:checkbox').prop('checked', false).click();

If you want to uncheck:

$('#MyCheckbox input:checkbox').prop('checked', true).click();

Summary

it's using a reverse process. for the first one it will make sure the checkbox property is NOT checked, then it will trigger the click which will change the checkbox property to 'checked' then it will fire the 'change' event - if it's bound.

like image 125
Harry S. Avatar answered Oct 19 '22 17:10

Harry S.