Need to understand how javascript functions are called
This is part of a code. The question is, does this line "$("#b_facetNewChild").button().click(function(){"
means that the following should be fired when the button "facetNewChild" is clicked? Because there is no "onClick" function at the button. Also, can you explain briefly, what does it mean to have this nested into another function "newChildFacet()" and how to call it?
Sorry, but I am new to javascript.Thanks!
function newChildFacet()
{
// button click
$("#b_facetNewChild").button().click(function(){
//get selected fId
var $fId=getSfS开发者_运维知识库electedFIds();
if($fId.length>0 && $fId.split(",").length!=1)
{
messageBox("Tip","Please select <b>ONE</b> as the parent facet. If no facet is selected, the new facet will be created under <b>root</b>.");
return false;
}
//some more stuff here!
});
// newChildFacetDialog
$("#newChildFacetDialog").dialog({
autoOpen: false,
modal: true,
title: "New Child Facet",
buttons: {
"Cancel": function() {
$(this).dialog('close');
},
"Create": function() {
//get data
var $parentId=getSfSelectedFIds();
});
$(this).dialog('close');
}
}
});
}
First, yes it bound as a click
event handler, so it'll run when #b_facetNewChild
is clicked - you won't see this in source, it's stored elsewhere in the DOM.
You can call it by invoking the click
event handlers on that element, like this:
$("#b_facetNewChild").click();
It doesn't matter that it's a nested/anonymous function, all we care about here is it's a click
event handler on that element, so you can trigger it any of the following ways:
$("#b_facetNewChild").click();
$("#b_facetNewChild").trigger("click");
$("#b_facetNewChild").triggerHandler("click");
You got it right.
$("#b_facetNewChild").button().click(function(){ ... })
does exactly what you mean. However, you won't see an onclick
handler in the HTML, but rather only if you inspect the DOM/JS element itself (for example, by using FireBug).
The fact that this call is located inside a function simply means it will only be called (i.e. callback attached to the button) once the wrapping function is called.
精彩评论