Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I trigger the `change` event of radio using jquery?

The page is here:

http://cistrome.org/cps/seqconfig?did=2693

And the original js codes are below(this one works well):

$(document).ready(function(){
    $(".open_gene").on('change', function(event) {
        $('#Gene_field').show();
    });

    $(".close_gene").on("change", function(event){
        $("#Gene_field").hide();
    });
});

So the .close_gene has an event handler for change. But when I want to trigger this event manually to hide the #Gene_field, like this:

>>> $('.close_gene').trigger("change")

In FireBugs, the returned value is:

[input#nolimit_radio.close_gene all]

But the #Gene_field is not hidden..

I was wondering that why I can't trigger change event which should already bind to function(event){ $("#Gene_field").hide();}. Does anyone have ideas about this? Thanks!

like image 979
Hanfei Sun Avatar asked Nov 19 '12 05:11

Hanfei Sun


People also ask

How do you trigger input change event?

var element = document. getElementById('just_an_example'); var event = new Event('change'); element. dispatchEvent(event); This will trigger event listeners regardless of whether they were registered by calling the addEventListener method or by setting the onchange property of the element.

How do you checked radio button is changed in jQuery?

Now, to detect when that radio box is changed, you can easily call the jQuery 'change' event inside your jQuery document ready function: $(document). ready(function(){ $('#my_radio_box'). change(function(){ alert('Radio Box has been changed!

How can add trigger click event in jQuery?

$( "#foo" ). trigger( "click" ); As of jQuery 1.3, . trigger() ed events bubble up the DOM tree; an event handler can stop the bubbling by returning false from the handler or calling the .


1 Answers

Try this:

$(".close_gene").click();

Its working fine for me in Firebug Console... :)

Update:

This should also work, but will not change the state of radio button

$(document).ready(function(){
  $(document).delegate(".open_gene",'change', function(event) {
     $('#Gene_field').show();
  });

  $(document).delegate(".close_gene", "change", function(event){
     $("#Gene_field").hide();
  });
});

$('.close_gene').trigger("change");
like image 163
rajukoyilandy Avatar answered Sep 29 '22 03:09

rajukoyilandy