Jump to content
Sign in to follow this  
hybrid kill3r

Changing Anchor Onclick By Id

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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...