Jump to content

Changing Anchor Onclick By Id


hybrid kill3r
 Share

Recommended Posts

I have an anchor that looks like this <a href='url' id='delete'> I want to have a JS script that changes the onclick of the delete anchor.

window.onload = deleteLinks();function verifyAction(url){		var verify = confirm("Are you sure you want to delete this item?  This action CANNOT be undone!");	if(verify){		window.location = url;	} else {		return false;	}	}function deleteLinks(){		var deleteButton = this.document.getElementById("delete");	deleteButton.onclick = "verifyAction('" + deleteButton.href + "');";	}

Error console in fire bug says that deleteButton is null. I can't figure out why this won't work.

Link to comment
Share on other sites

Hey Hybrid killer, here is a working version:

window.onload = deleteLinks;function verifyAction(url){    var verify = confirm("Are you sure you want to delete this item?  This action CANNOT be undone!");    if(verify){        window.location = url;    }else     {        return false;    }}function deleteLinks(){    // Does work with the this keyword but this keyword is unnecessary.    var deleteButton = document.getElementById("delete");    // This allows us to pass the deleteButton.href as a parameter to verifyAction.    deleteButton.onclick = function(){return verifyAction(deleteButton.href)};}

Hope this helps! (Tested in IE8, FF3, Chrome 4).

Edited by Sckewi
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
 Share

×
×
  • Create New...