anony_root
anony_root

Reputation: 1527

How to add onclick="return func()" by addEventListener?

I have a link: <a href="/somelink" id="link_test" onclick="return func(event);">Link</a>.

I want to remove this onclick attribute and add a click event listener by addEventListener.

I tried:

ge("link_test").addEventListener('click',function(){
  return func(window.event);
},false);

also:

ge("link_test").addEventListener('click',"return func(window.event);",false);

No result. Is it possible to do it by addEventListener (and how)?

PS: actually I have a cross-browser variant of addEventListener func, but not important in this case.

Upvotes: 1

Views: 206

Answers (1)

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

Try:


var anch = document.getElementById("link_test");
anch.addEventListener ("click", function (event) {
    event.preventDefault();
    func(event) 
}, false);

Do you mean something like this

Upvotes: 1

Related Questions