Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > javascript confirm delete

Reply
Thread Tools

javascript confirm delete

 
 
neoconwannabe
Guest
Posts: n/a
 
      11-02-2004
Hi,
I have an html form with a number of checkboxes that can be checked
to delete
items. I want to have a javascript alert prompt box to pop up to
confirm the delete.
I am building my site with javascript as an enhancement and not a
requirement
to work.
My question is can I have the html form function normally if the user
has javascript
disabled? It would act as thought there were no javascript code
involved in the
form at all then.

Thanks,
John
 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      11-02-2004
neoconwannabe wrote:
> Hi,

[...]
> My question is can I have the html form function normally if the user
> has javascript
> disabled? It would act as thought there were no javascript code
> involved in the
> form at all then.


Yes.

My reading was that you wanted the delete confirmed if JS was enabled,
but if it wasn't, the form just submits. Here is a script that
confirms when a user checks a box, but doesn't confirm when they
uncheck it. If they cancel the delete action, the box is unchecked.

When the form is submitted, if JS is enabled, the hidden checkbox js is
checked so you know at the server that they have already confirmed the
delete. If they don't have JS enabled, then you know they haven't been
given the prompt because the JS checkbox will not be checked.

I've used a label so you can click on the label text or the checkbox to
check/uncheck it.

Be careful with the reference to the label text. If you put the text
before the checkbox, then "a.nextSibling" will be undefined and an
error will result. You may want to use
"a.parentNode.childNodes[0].data" if the text is before the checkbox,
or "a.parentNode.childNodes[1].data" if you put the text after the
checkbox. I just like nextSibling because it's more concise but you
must put the label text after the checkbox.

*However*, as Andrew advises, if this is for a web site, the confirm
prompt is a bad idea and you should use a summary page to confirm what
the user wants. Yes, it's more laborious, but much more reliable and
if there are a lot of options, they all get confirmed in one go rather
than lots of separate confirms. Consider whether some other method of
confirmation is suitable, like such as a visual prompt in the page
(change the text label to grey or use strike-through or similar).

Cheers, Rob.

<script type="text/javascript">
function doIt(a) {
var b = (confirm('This will delete ' + a.nextSibling.data
+ '\nClick \'OK\' to confirm'));

if(b){
// Do the delete actions
} else {
a.checked=false;
}
}
</script>
<p>Click on a checkbox to delte the option</p>
<form action="">
<input type="checkbox" name="js" style="display: none;">
<label><input type="checkbox" name="opt1"
onclick="if(this.checked)doIt(this);">Option 1</label><br>
<label>Option 2<input type="checkbox" name="opt2"
onclick="if(this.checked)doIt(this);"></label><br>
<input type="reset">
<input type="submit" onclick="this.form.js.checked=true">
</form>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re:how i make a window confirm to delete at datagrid? Saravana ASP .Net 1 10-04-2004 03:27 PM
how i make a window confirm to delete at datagrid? Dexter ASP .Net 2 10-03-2004 10:43 PM
Confirm Delete popup from a LinkButton in a repeater Sandy ASP .Net 1 06-11-2004 05:37 PM
confirm('delete?') doesn't work on ie6 (windows xp) Spongebob ASP .Net 2 02-11-2004 09:38 PM
confirm box when clicking Delete Linkbutton in DG Calvin Lai ASP .Net 4 02-04-2004 03:03 PM



Advertisments