Andy
Andy

Reputation: 992

How to trigger an event in jquery if an element is changed programmatically?

I have several checkboxes with the same classes. When I checked the box also checked other checkboxes with the same classes. Also i have event handler change for checkboxes. After checked checkbox checked and others, all OK. But event handler called once. I need that called this event for each checkbox if this checkbox changed. How to do it?

My event handler: $('input[type="checkbox"]').on 'change', checkboxClick

This checked or unchecked checkbox $(className).not(this).prop('checked', this.checked) - this also changed checkboxes with the same ClassName, but for others except this event handler not called.

Upvotes: 0

Views: 58

Answers (2)

guest271314
guest271314

Reputation: 1

You can use Function.prototype.call() to call named function checkboxClick within event handler, instead of dispatching change event

$(className).not(this).prop("checked", this.checked).each(function() {
  checkboxClick.call(this)
});

Upvotes: 1

Prashanth Reddy
Prashanth Reddy

Reputation: 197

use .each function to apply all instances of that class.

$(className).each(function(){
    $(this).prop('checked', true)
});

Upvotes: 0

Related Questions