LefterisL
LefterisL

Reputation: 1143

Verification on button click before calling JS function

Ok i know how to ask for verification when a button is clicked but how can i make it so that from the same button on click it'll first ask for verification and then do the JS function i want?

for example

function clicked(e)
{
    if(!confirm('Are you sure you want to do this?'))e.preventDefault();
}

<button type="button" onclick="clicked(event)">Close Round 1</button>

What i want to do is after the confirmation is given to call the below function,

function closer1() {...}

Obviously there is not one button, can i somehow pass the id of the button to the clicked function and somehow call the function from there?

Upvotes: 0

Views: 509

Answers (6)

Vince
Vince

Reputation: 1851


Here's what I would do:


HTML:

<button type="button" onclick="sample_one(event)">Close Round 1</button>

JS:

function sample_one(e)
{
    e.preventDefault(); // Stops default action

    // Ask for confirmation
    if ( confirm('Are you sure you want to do this?') )
    {
        sample_two(); // Call the second defined function
    }
}

function sample_two()
{
    alert('sample_two function called.'); // Alert message to show defined function call
}

Here is a sample jsfiddle


Upvotes: 1

airi
airi

Reputation: 585

function clicked(e){

   e.preventDefault();  

    var r = confirm("Are you sure you want to delete?");
    if (r == true) {

    } else { 
    return false;

    }

    }

Upvotes: 0

Aashray
Aashray

Reputation: 2763

Why not just do this?

<button type="button" onclick="closer1()">Close Round 1</button>

If thats not what you want to call, then you could do something like this:

<button type="button" onclick="clicked('button1')">Close Round 1</button>

Javascript

function clicked(str){
   if(str=='button1')
      closer1();
   if(str=='button2')
      closer2();
   //and so on
}

Upvotes: 1

Tijme
Tijme

Reputation: 99

function clicked(event, element)
{
   if(!confirm('Are you sure you want to do this?'))
   {
      e.preventDefault();
      return false;
   }

   // you can use 'element' as your element that has been clicked
}

<button type="button" onclick="clicked(event, this)">Close Round 1</button>

Upvotes: 1

SubjectCurio
SubjectCurio

Reputation: 4872

var result = confirm("Are you sure you want to do this?");
if (result == false) {
  e.preventDefault(); // user pressed cancel
} else {
  closer1(); // user pressed ok
}

If your clickable buttons share a function, you could implement something like this, where the result varies depending on the id of the button clicked

var result = confirm("Are you sure you want to do this?");
if (result == false) {
  e.preventDefault(); // user pressed cancel
} else if ($(this).attr('id') == 'button1') {
  closer1(); // user pressed ok, and request came from button 1
} else if ($(this).attr('id') == 'button2') {
  closer2(); // user pressed ok, and request came from button 2
}

Upvotes: 1

Scary Wombat
Scary Wombat

Reputation: 44834

What is wrong with

if(!confirm('Are you sure you want to do this?'))
{
   e.preventDefault();
   return false;
}
if(e=='button1') {
    closer1();
}

Upvotes: 1

Related Questions