Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Alert box with OK and cancel

Status
Not open for further replies.

emozley

Technical User
Jan 14, 2003
769
GB
Hi,

I have an ASP page with dynamically generated links. Each link is for deleting the item next to it. I am trying to set it up so that when you click Del you get an alert box that asks you if you are sure. If you click OK it takes goes to the page that deletes the item and if you click Cancel it goes back to the page you were just on (with the list of items with Del next to them).

In <HEAD></HEAD> I have:

<script language="javascript" type="text/javascript">
<!--
function AlertBox2() {
if(confirm("Confirm Delete"))
{
}
}
//-->
</script>

And my links look like this:

<a href='deletecase.asp?CaseID=57' onclick='AlertBox2()'>Del</a>

The problem I had was that it would go to the deletecase.asp page whether I clicked OK or Cancel. What do I need to add to the function to make it work properly?

I am a total beginner at Javascript so apologies for the potential stupidity/simplicity of the question!

cheers

Ed
 
Change:

<a href='deletecase.asp?CaseID=57' onclick='AlertBox2()'>Del</a>

to:

<a href='[red]javascript:void(0)[/red]' onclick='AlertBox2()'>Del</a>

This disables the link so that it only responds to the onclick.

Now update the AlertBox2() to:
Code:
function AlertBox2() {
 if(confirm("Confirm Delete"))
 {
  [b]document.location = 'deletecase.asp?CaseID=57';[/b]
 }
}

'hope that helps.

--Dave


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
O Time, Strength, Cash, and Patience! [infinity]
 
keep in mind this is not a complete solution. you should definitely put some code server-side to verify as well, because anyone with JS disabled will not see those confirm boxes.

*cLFlaVA
----------------------------
[tt]your mom goes to college[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
[banghead]
 
I like cLFlaVA's response better!

--Dave


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
O Time, Strength, Cash, and Patience! [infinity]
 
Thanks for the responses - cLFlaVA I've gone with your approach but unfortunately I still have the same problem. If I click the cancel button it's still executing the deletecase.asp script.

Any ideas?

cheers
 
I've tried it with an even smaller example and get the same problem:

<script language="javascript" type="text/javascript">
<!--
function AlertBox2() {
return confirm('confirm delete');
}

//-->
</script>
<a href="freetime.jpg" onclick="AlertBox2()">View picture</a>

the link takes you to the picture regardless of whether you click OK or cancel.
 
cLFlaVA said:
[tt]onclick="return AlertBox2();"[/tt]

Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
 
Poor effort on my part there - apologies. Now fully operational!

cheers

Ed
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top