Jump to content

Changing Anchor Onclick By Id


hybrid kill3r

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).

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...