What's the difference between
$("<a>", {
"id" : "myId",
"text" : "my link",
"href" : "#",
"onclick" : function() { return false; }
);
and
$("<a>", {
"id" : "myId",
"text" : "my link",
"href" : "#",
"click" : function() { return false; }
);
?
What's the difference between
$("<a>", {
"id" : "myId",
"text" : "my link",
"href" : "#",
"onclick" : function() { return false; }
);
and
$("<a>", {
"id" : "myId",
"text" : "my link",
"href" : "#",
"click" : function() { return false; }
);
?
Share Improve this question asked Dec 13, 2012 at 4:20 supertonskysupertonsky 2,7438 gold badges39 silver badges78 bronze badges1 Answer
Reset to default 17Using onclick
creates an attribute, and its value should be a string that refers to a function, not an actual function. Using click
creates a property on the element, and its value should be the function itself.
So, the first one is written incorrectly; should be like this:
$("<a>", {
"id" : "myId",
"text" : "my link",
"href" : "#",
"onclick" : "somefunction()"
} );
where "somefunction" is defined in the global scope:
window.somefunction = function() { return false; }