I'm trying to set an onclick attribute to an anchor like this:
$("a").click(function(e) {
$(this).onclick = "ItWorks";
output = "JS onclick attempt: my onclick is: <strong>" + $(this).onclick + "</strong>";
$(this).prop("onclick", "ItWorks");
output += "<br />jQuery prop() attempt: my onclick is: <strong>" + $(this).prop("onclick") + "</strong>";
$(this).attr("onclick", "ItWorks");
output += "<br />jQuery attr() attempt: my onclick is: <strong>" + $(this).attr("onclick") + "</strong>";
alert(output);
e.preventDefault();
});
I've tried pure JS, jQuery prop() and attr() functions. As you can see on this example, only .attr() function works as it should.
Can someone tell me what am I doing wrong that the other two attempts return "null" or "undefined"?
$a? also onclick is a bad idea! because it is an event!