I need to clone the onclick event of an element from one link to another.
<a id='foo' onclick='bar'>
I need some javascript or jquery that will return `bar'.
$('#foo').attr('onclick')
//returns 'undefined'
Use the native .getAttribute method of the DOM element.
I have compared .attr('onclick') to [0].getAttribute('onclick'):
function onclick(event) {bar;} (jQuery version 1.5.2, included by Stack Overflow)"bar"Test case (copy & paste in console):
$('<a id="foo" onclick="bar">')[0].getAttribute('onclick')
bar$().jquery). See also, this fiddle: jsfiddle.net/3qkJrIt returns bar just fine in jQuery 1.6.4, where the prop method returns the full function instead. Solution would be to upgrade your jQuery to the newest version.
undefined: jsfiddle.net/Z4gAp. What browser are you using? (I'm using Chrome on a Mac)