Trying to call an exchange method set by jQuery

In my code, I am setting a change listener to my checkboxes here.

  $(".section").change(function() { 
       if($(this).is(":checked")) {
        $("." + this.id).show();
        ...

      

Now I am trying to do a "code driven" click on a checkbox and I have

  $(".secTitle").click(function(e) {
    var elem = this;
    while(elem) {
      if (elem.className && elem.className.indexOf ('DOC_SECTION') != -1) {
        var clzes = elem.className.split(" ");
        var clzIdx = 1;
        if (elem.getAttribute('closeSecClassIdx')) {
          clzIdx = parseInt(elem.getAttribute('closeSecClassIdx'));
        }
        var chk = document.getElementById(clzes[clzIdx]);
        chk.checked = false;
        alert(chk.onchange);
        //chk.changed();
        break;
      }
      else {
        elem = elem.parentNode;
      }
    }
  });

      

I know I have the correct item as it chk.checked = false;

works correctly. After that, I try to call the change method I set earlier, but my warning shows "undefined".

+3


source to share


1 answer


You can trigger the event change

by calling $(chk).change()

. Below I've created a small prototype that shows binding to an event change

and calls it.



jQuery(function($) {
    // bind to the change event
    $("input").change(function() {
        console.log('change triggered!');
    });
    
    // now trigger it
    $("input").change();    
});
      

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" />
      

Run codeHide result


+1


source







All Articles