Open In App

How to display confirmation dialog when clicking an <a> link using JavaScript / jQuery ?

Given <a> element and the task is to display the confirmation message when clicking the <a> link, with the help of JavaScript and jQuery.

Display confirmation dialog when clicking an <a> link using JavaScript



Example 1: This example adds a confirm() method to the link with onclick event. This will verify whether you want to proceed or not.




<!DOCTYPE HTML> 
<html
    <head
        <title
            Display a confirmation dialog when
            clicking an a tag link
        </title>
    </head
      
    <body style = "text-align:center;">
           
        <h1 style = "color:green;"
            GeeksForGeeks 
        </h1>
          
        <p id = "GFG_UP" style =
            "font-size: 15px; font-weight: bold;">
        </p>
          
        <a href="#" onclick="return confirm('Are you sure?')">
            Link
        </a>
          
        <br><br>
          
        <p id = "GFG_DOWN" style
            "color:green; font-size: 20px; font-weight: bold;">
        </p>
          
        <script>
            var el_up = document.getElementById("GFG_UP");
              
            el_up.innerHTML = 
                "Click on the LINK for further confirmation."; 
        </script
    </body
</html>                    

Output:

Example 2: This example adds a class confirm to all link. After this, it adds the EventListener to the elements of that class, on onclick event. Then it calls a method to separately handle the confirm dialog.




<!DOCTYPE HTML> 
<html
    <head
        <title
            Display a confirmation dialog when
            clicking an a tag link
        </title>
    </head
      
    <body style = "text-align:center;">
           
        <h1 style = "color:green;"
            GeeksForGeeks 
        </h1>
          
        <p id = "GFG_UP" style =
            "font-size: 15px; font-weight: bold;">
        </p>
          
        <a href="#" class="confirm">Link</a>
          
        <br><br>
          
        <p id = "GFG_DOWN" style =
            "color:green; font-size: 20px; font-weight: bold;">
        </p>
          
        <script>
            var el_up = document.getElementById("GFG_UP");
                  
            el_up.innerHTML =
                    "Click on the LINK for further confirmation."; 
              
            var el = document.getElementsByClassName('confirm');
              
            var confirmThis = function (e) {
                if (!confirm('Are you sure?')) e.preventDefault();
            };
              
            for (var i = 0, l = el.length; i < l; i++) {
                el[i].addEventListener('click', confirmThis, false);
            }
        </script> 
    </body
</html>                    

Output:

Display confirmation dialog when clicking an <a> link using jQuery

Example 1: This example adds a class confirm to the all link. After this, It adds the EventListener to the elements of that class, on onclick event. Then it calls the confirm dialog.




<!DOCTYPE HTML> 
<html
    <head
        <title
            Display a confirmation dialog when 
            clicking an a tag link
        </title>
          
        <script src =
        </script>
    </head
  
    <body style = "text-align:center;">
           
        <h1 style = "color:green;"
            GeeksForGeeks 
        </h1>
          
        <p id = "GFG_UP" style =
            "font-size: 15px; font-weight: bold;">
        </p>
          
        <a href="#" class="confirm">Link</a>
          
        <br><br>
          
        <p id = "GFG_DOWN" style
            "color:green; font-size: 20px; font-weight: bold;">
        </p>
          
        <script>
            $("#GFG_UP").
                text("Click on the LINK for further confirmation."); 
                  
            $('.confirm').on('click', function () {
                return confirm('Are you sure?');
            });
        </script
    </body
</html>                    

Output:

Example 2: This example adds a confirm() method to the link with onclick event. This will verify whether you want to proceed or not.




<!DOCTYPE HTML> 
<html
    <head
        <title
            Display a confirmation dialog when 
            clicking an a tag link
        </title>
          
        <script src =
        </script>
    </head
      
    <body style = "text-align:center;"
      
        <h1 style = "color:green;"
            GeeksForGeeks 
        </h1>
          
        <p id = "GFG_UP" style
            "font-size: 15px; font-weight: bold;">
        </p>
          
        <a href="#" onclick="return confirm('Are you sure?')">
            Link
        </a>
          
        <br><br>
          
        <p id = "GFG_DOWN" style
            "color:green; font-size: 20px; font-weight: bold;">
        </p>
          
        <script>
            $("#GFG_UP").
                text("Click on the LINK for further confirmation."); 
        </script
    </body
</html>                    

Output:

jQuery is an open source JavaScript library that simplifies the interactions between an HTML/CSS document, It is widely famous with it’s philosophy of “Write less, do more”.
You can learn jQuery from the ground up by following this jQuery Tutorial and jQuery Examples.


Article Tags :