Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery: Remove "OnClick" events from 'a' tags

Tags:

jquery

This is a weird question. We have some production links down on our intranet. Some rouge javascript is returning a "false" on all links on our intranet homepage.

We don't have access to the source to re-build the control and fix this javascript.

So as a temporary band-aid I want to move all the "onClick" events on the links using jquery

The links right now are like this:

<a href="https://www.mysite.com/" onclick="AddHit('Header: mysite.com', 'http://www.mysite.com'); return false;" title="Mysite Home">www.Mysite.com</a>

I want to write some jquery to get ride of the "onclick='AddHit..." code.

like image 527
Nate Avatar asked Dec 19 '11 17:12

Nate


People also ask

How do I remove onClick from an element?

To remove an element from the DOM onclick in JavaScript: Select the DOM element with a method like getElementById() . Add a click event listener to the element. Call the remove() method on the element in the event handler.

How to delete onClick with jQuery?

Answer: Use the jQuery off() Method You can simply use the jQuery off() method to remove the event handlers that were attached with on() .

How do I delete an onClick event in react?

We set an event handler to the onClick event of the button, so when the button is clicked, the handler function will be called. In the handler function, we use the setState function for the visibility state to update the state. const removeElement = () => { setVisible((prev) => ! prev); };


1 Answers

You can try this:

$("a").removeAttr("onclick");

Edit Updating answer based on comments and jquery Docs:

Note: Removing an inline onclick event handler using .removeAttr() doesn't achieve the desired effect in Internet Explorer 6, 7, or 8. To avoid potential problems, use .prop() instead

Best way to do this:

$("a").prop("onclick", null);
like image 153
Rondel Avatar answered Sep 30 '22 03:09

Rondel