Solved

Confirm delete with OnClick

Posted on 2003-11-12
8
1,728 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
Comment Utility
Oh, I also want to cancel the event if the user chooses cancel.
0
 
LVL 10

Expert Comment

by:Bustarooms
Comment Utility
<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
Comment Utility
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
 
LVL 10

Accepted Solution

by:
Bustarooms earned 100 total points
Comment Utility
<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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 2

Expert Comment

by:_DanElliott
Comment Utility
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
Comment Utility
Awesome, here's 100 points.  Thanks a lot!
0
 
LVL 10

Expert Comment

by:Bustarooms
Comment Utility
no problem
0
 
LVL 1

Expert Comment

by:oktexasguy
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now