Rewriting confirmation box from javascript to jquery

Posted on 2009-12-29
Last Modified: 2012-06-21
Dear experts,

Below is a simple confirmation box done in javascript.

- An example with and without an "else"-statement.
- An example that only affects the button, not the whole form.
- An example that redirects to a specified page (like in window.location)

I'm changing all my javascript into jquery, so how do I rewrite this piece into jQuery?

Library: <script type='text/javascript' src=''></script>

<input type="button" onclick="confirmation()" value="Delete record" />

<script type="text/javascript">
function confirmation() {
    var answer = confirm("WARNING: The record will be deleted permanently. Continue?")
    if (answer){
        window.location = "deleterecord.asp?id=<%= rs("id") %>";
        alert("Record not deleted.")

Open in new window

Question by:DanishCoder
    LVL 22

    Accepted Solution

    few things remain same.. hope this helps.

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
      <script src=""></script>
      <script type="text/javascript">
        $("#deleteButton").click(function () { 
          var answer = confirm("WARNING: The record will be deleted permanently. Continue?")
    		if (answer){
    			window.location = "";
    			alert("Record not deleted.");
    	$("#deleteButton1").click(function () { 
          var answer = confirm("WARNING: The record will be deleted permanently. Continue?")
    		if (!answer){
    			alert("Record not deleted.");
    		window.location = "";
    	<input type="button" id="deleteButton" value="Delete Record 1">
    	<input type="button" id="deleteButton1" value="Delete Record 2">

    Open in new window


    Author Closing Comment

    Thx Kadaba! I deletede the else-statement in line 13-15 to fit my needs.
    Thought it could be done much easier in jQuery than "almost the same" ;-)
    Happy New Year!

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
    Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
    In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
    The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

    745 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

    16 Experts available now in Live!

    Get 1:1 Help Now