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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Confirm and Alert .. simple question

Status
Not open for further replies.

allyeric

Programmer
Mar 14, 2000
104
CA
Hi

I would like to a confirm and/or an alert box on my page. I will post my code below, just can't figure out why it doesn't work .. very new to javascript.
<HEAD>
<SCRIPT>
<!--- Hide script from old browsers
function validate(f)
{
   if (confirm(&quot;Are you sure?&quot;))
alert(&quot;Deleted&quot;)    
   else
      alert(&quot;Please come back again.&quot;)
}
// end hiding from old browsers -->
</SCRIPT><BR>
</head>


and then .. I want to use an onClick event to trigger ..

<input type=&quot;button&quot; value=&quot;Delete&quot; name=&quot;B2&quot; onClick=&quot;validate(this.form)&quot;>


I end up with two error messages .. the first one for line
if (confirm(&quot;Are you sure?&quot;)) .. error saysInvalid Character

Second message in on the onClick line .. Object Expected

I am sure this is an easy problem, I know that when someone is kind enough to help me, I will just look and say duhhhh! (mind not functioning today - it IS friday, after all!!

Any help in this is greatly appreciated!

[sig][/sig]
 
ok .. I've narrowed down my problem a little better.

in the OnClick I have:

<input type=&quot;button&quot; value=&quot;Delete&quot; name=&quot;B2&quot; onClick=&quot;Javascript: confirm('Are you sure you want to delete this product?');&quot;>


this seems to work fine .. but now I need to know how I can tell what the user clicked? Please help!!
[sig][/sig]
 
Hi,

Your initial code didn't generate any errors in IE 4. Which browser are you using? There isn't any reason to pass the form to the validate() function you have since you don't use it. (or was this just a scaled down version?).

Also, you don't need to precede javascript code with 'javascript:' inside form events.

Anyhow, here's the slightly modified code function code:

<SCRIPT>
<!--- Hide script from old browsers
function validate()
{
if (confirm(&quot;Are you sure?&quot;))
alert(&quot;Deleted&quot;)
else
alert(&quot;Please come back again.&quot;)
}
// end hiding from old browsers -->
</SCRIPT>

...

<input type=&quot;button&quot; value=&quot;Delete&quot; name=&quot;B2&quot; onClick=&quot;validate()&quot;>

Here's how you could do it all in the onClick event:

<input type=&quot;button&quot; value=&quot;Delete&quot; name=&quot;B2&quot; onClick=&quot;if (confirm('Are you sure you want to delete this product?')) { alert('Deleted');} else {alert('Please come back again');}&quot;>

Of course, you can use whitespace in here to make it more readable.

Regards, [sig]<p>Russ<br><a href=mailto:bobbitts@hotmail.com>bobbitts@hotmail.com</a><br><a href= is in</a><br>[/sig]
 
Thanks Russ .. it works great now .. I don't know why it didn't the first time. Much appreciated! [sig][/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top