Forum Moderators: open

Message Too Old, No Replies

want delete confirmation popup window when hyperlink is clicked

Confirmation window in javascript

         

dolphine

10:08 pm on Mar 12, 2009 (gmt 0)

10+ Year Member



Hi, guys.
I have tryied to use this code, when I have to show the confirmation window, before deleting some data.
I couldn't saw any confirmation and I wondered how such not workable code is still posted and misleading other people.
One wise man helped me to get understanding in this matter.
You should use:
confirm.value = window.confirm("Are you sure you want to delete?");
if you want to get some positive result.
So my workable code is:

<script language="javascript" type="text/javascript" >
function confirmDelete() {
var confirm = document.getElementById("confirm");
confirm.value = window.confirm("Are you sure you want to delete?");
return confirm.value;
}
</script>

<form enctype="multipart/form-data" action="delPicture.php" method="post" onsubmit="return confirmDelete();">
<div id="Triggered"><b>Delete by Id</b></div>
<input type="hidden" name="confirm" id="confirm" value="false">
<input name="recordId" type="text" value="" /><br>
<input type="submit" value="Delete"/>
</form>

LifeinAsia

10:12 pm on Mar 12, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Try:
<form enctype="multipart/form-data" action="delPicture.php" method="post" onSubmit="return confirmDelete();">

JavaScript is case sensitive: onsubmit is not the same as onSubmit.

[edited by: LifeinAsia at 10:13 pm (utc) on Mar. 12, 2009]

Fotiman

4:38 am on Mar 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



@LifeinAsia
Event handler attributes in HTML are NOT case sensitive, unless you are using XHTML in which case they MUST be all lower case. In other words, it's always better to use all lower case for inline event handlers.

@dolphine
The problem is that this:


return confirm.value;

is returning a string value of "false", which does not equate to the boolean value of false. Try this instead:

return (confirm.value == "true");

Fotiman

1:56 pm on Mar 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Alternatively, you could do this:

function confirmDelete() {
var confirm = document.getElementById("confirm");
var bConfirm = window.confirm("Are you sure you want to delete?");
confirm.value = bConfirm;
return bConfirm;
}

GaryK

4:21 pm on Mar 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry for butting in here. I'm hardly a JS expert. I have been writing code for a living since 1970 though. I feel the need to ask the question I dotted-out above.

Apart from it not being good form, are there any problems associated with using confirm as a variable name and a hidden form element name when it's already a built-in function name?

Fotiman

3:33 am on Mar 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



confirm is actually a method of the window object. So it can be accessed by calling window.confirm, or simply by calling confirm if it doesn't conflict with other variables (it does conflict in dolphine's code, so must be accessed through the window object). I agree, it is probably not good form to name you internal variables such that they could conflict with window object methods. Here is another alternative to the code:

function confirmDelete() {
var c = document.getElementById("confirm");
var bConfirm = confirm("Are you sure you want to delete?");
c.value = bConfirm;
return bConfirm;
}

GaryK

4:18 am on Mar 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for the clarification. :)

coopster

10:58 am on Mar 14, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I agree, it is probably not good form to name your internal variables such that they could conflict with window object methods.

FWIW:
[developer.mozilla.org...]