Jump to content

JS Event Listeners


hybrid kill3r

Recommended Posts

So, I have a javascript function that when a user clicks on a link with this attached, it opens a confirmation asking if they want to continue. But instead of using onclick everytime I want to use it, I want to use something like rel="confirm" on the anchor. How would I do this?

function confirmAction(hyperlink){    if(confirm(("Are you sure you want to do this?  This action CANNOT be undone!!!")){	    	    window.location = hyperlink;	        } else {	    	    return false;        }}

Link to comment
Share on other sites

After the page is loaded, loop through every <a> element using document.getElementsByTagName(). If the "rel" property is "confirm" then attach an event listener to the link.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...