This question is actually related to How to remove "onclick" with JQuery?.
<div onclick="doSomething();">Click me</div>
According to the referenced article, the correct way to remove this inline event for jQuery 1.7+ would be:
// There's only one div in this example
$( "div" ).prop( "onclick", null ).off( "click" );
Say I wanted to add an inline onclick event later on. I tried adding the event with prop
and it did not work. However, using attr
to add said event did work.
// Does not add event
$( "div" ).prop( "onclick", "doSomething();" ).on( "click ");
// Neither does this
$( "div" ).prop( "onclick", "doSomething();" );
// This one works
$( "div" ).attr( "onclick", "doSomething();" );
// This one works, too
$( "div" ).attr( "onclick", "doSomething();" ).on( "click" );
Can anyone shed some light on this?
For removing inline events, the current recommended approach with jQuery 1.7+ is prop
. Yet, for adding inline events, prop
does not seem to respond. Is prop
supposed to function for adding inline events or we only use attr
for that purpose? That would seem inconsistent, if true: remove with prop
but add with attr
.
Thank you for any help in advance.