Reputation: 149
So I have got a hyperlink, which if pressed does a major action ( delete something from the database ) so I want a confirm box once it is pressed so that they dont make mistakes.
My code for the hyperlink is:
<a href='*****.php?Number3=1222&AssignedTo=$12331'>[x]</a>
I am unsure on Javascript, and I know this has a major part in it... Please help? PS The hyperlink's URL is random, and there are many of them so please dont make it so that it only works with one link
Upvotes: 5
Views: 11517
Reputation: 4122
Here's an answer for newer jQuery with some extra functionality for those that want custom confirm messages.
$('body').on('click', '[data-confirm]', function(){
var msg = $(this).attr('data-confirm');
return confirm(msg);
});
You use data-confirm instead of .confirm. Your HTML appears as follows:
<span data-confirm="Are you sure?">delete</span>
Upvotes: 0
Reputation: 227220
Add a class to the a tags that means you want a confirm box.
<a href="file.php" class="confirm">Link</a>
Then attach an event handler to those:
var links = document.getElementsByClassName('confirm');
for (var i = 0; i < links.length; i++) {
links[i].onclick = function() {
return confirm("Are you sure?");
};
}
Or using jQuery:
$('.confirm').live('click', function(){
return confirm("Are you sure?");
});
Upvotes: 0
Reputation: 37464
Give your href a class name and target it with something like jQuery/straight JS, then do this:
var r=confirm("Are you sure you want to delete?");
if (r==true){
return true;
{
else {
return false;
}
Upvotes: 0
Reputation: 1481
try
<a href='*****.php?Number3=1222&AssignedTo=$12331' onclick="return confirm('Are you sure you want to delete?')" >[x]</a>
Upvotes: 8