Solved

Confirm delete with OnClick

Posted on 2003-11-12
8
1,737 Views
Last Modified: 2008-03-06
Hello all.

I have an app in which the user can click "Delete" to delete a record from a database.  When clicked, I want to confirm from the user.  Being a novice JS programmer, I can't get it to work.  I'm sure it will be easy for you experts out there.  Here's how I'm trying to do it:

<a href="ddGalDelete.asp?galItem=<%=rsGallery("galItemID")%>" title="Delete This Listing" onClick="Confirm('Are you sure that you want to delete this item?')";>Delete</a>

As a bonus question, can anyone tell me why 'alert' works here:
<a href="ddGalDelete.asp?galItem=<%=rsGallery("galItemID")%>" title="Delete This Listing" onClick="Alert('Are you sure that you want to delete this item?')";>Delete</a>
0
Comment
Question by:knottydrd
8 Comments
 

Author Comment

by:knottydrd
ID: 9734244
Oh, I also want to cancel the event if the user chooses cancel.
0
 
LVL 10

Expert Comment

by:Bustarooms
ID: 9734334
<a href="ddGalDelete.asp?galItem=<%=rsGallery("galItemID")%>" title="Delete This Listing" onClick="if(!confirm('Are you sure that you want to delete this item?'))return false;">Delete</a>


Alert will not work because Javascript is case sensitive...the proper format for an alert is alert('what do you want to say');

you had the same problem with your confirm()
0
 

Author Comment

by:knottydrd
ID: 9734429
Thanks

I switched to this:

<script type="text/javascript">
     function ConfirmDelete(){
         var confDel = confirm("Enter some information")
     }
</script>

            <span class="tableLinks"><a href="ddGalDelete.asp?galItem=<%=rsGallery("galItemID")%>" title="Delete This Listing" onClick='ConfirmDelete()'";>Delete</a>


How do I cancel the click if the user chooses "Cancel" on the confirm box?
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 10

Accepted Solution

by:
Bustarooms earned 100 total points
ID: 9734442
<script type="text/javascript">
     function ConfirmDelete(){
         var confDel = confirm("Enter some information")
         return confDel
     }
</script>

          <span class="tableLinks"><a href="ddGalDelete.asp?galItem=<%=rsGallery("galItemID")%>" title="Delete This Listing" onClick='return ConfirmDelete()'";>Delete</a>
0
 
LVL 2

Expert Comment

by:_DanElliott
ID: 9734459
Change it to

<span class="tableLinks"><a href="ddGalDelete.asp?galItem=<%=rsGallery("galItemID")%>" title="Delete This Listing" onClick='return ConfirmDelete()'";>Delete</a>

Add "return" to the onClick
0
 

Author Comment

by:knottydrd
ID: 9734463
Awesome, here's 100 points.  Thanks a lot!
0
 
LVL 10

Expert Comment

by:Bustarooms
ID: 9734470
no problem
0
 
LVL 1

Expert Comment

by:oktexasguy
ID: 9734549
knottydrd-
Try this out.  It uses a different method for page forwarding but will do exactly what you are trying to do above.

<script type="text/javascript">
function ConfirmDelete(objectId){
    if( confirm("Are you sure you want to delete?") ){
        var newLoc = "ddGalDelete.asp?galItem=" + objectId
        window.location = newLoc;
    }
}
</script>

<a href="javascript:ConfirmDelete('<%=rsGallery("galItemID")%>');" title="Delete This Listing">Delete</a>
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question