How does jQuery handle the click event?

I'm new to jQuery and can't seem to get the selector to work with the element id. Below is the code:

$(function() {  
  /* $("#ShowBox").onclick = ShowAccessible;  */  
  document.getElementById("ShowBox").onclick = ShowAccessible;  
  /* $(".searchcheck").click = ShowAccessible; */  
});

function ShowAccessible() {  
  $("tr.hide").fadeOut();  
}

      

However, none of the two commented out lines work, i.e. they do not apply a click event to a checkbox named ShowBox with class "searchcheck". Why is this?

+1


a source to share


3 answers


JQuery uses functions, not properties, to set handlers.



$ ("# ShowBox"). Click (ShowAccessible); 
+6


a source


no onclick property for jQuery object; when you use $([selector])

, a jQuery object is returned, not an element.

Use

.click(function () { ...}); // can be an anonymous function or named function

      

or



.bind('click', function() { ... }); // can be an anonymous function or named function

      

So

$("#ShowBox").click(ShowAccessible);
$("#ShowBox").bind('click', ShowAccessible);

      

+8


a source


If you really had to do it with an assignment, I think

$ ("# ShowBox") [0] .onclick = ShowAccessible;

will work.

0


a source







All Articles