Dean Jason
Dean Jason

Reputation: 105

confirm event within click event returned error

  $('#reset').click(function(){
    var confirm = confirm("This will reset everything, do you really want to continue?!");
if (confirm == true) {
    alert(); 
} 

  });

Any idea why above code doesn't work? I got an error of undefined is not a function.

Upvotes: 0

Views: 46

Answers (4)

Muhammmad Hadi Rajani
Muhammmad Hadi Rajani

Reputation: 611

You should try by changing the name of your variable "confirm" (var confirm). It is the keyword of javascript language.

Upvotes: 0

dev1234
dev1234

Reputation: 5706

Confirm is a function, please change the variable confirm in to something else.

var result = confirm("This will reset everything, do you really want to continue?!");

Upvotes: 0

jdphenix
jdphenix

Reputation: 15425

That's because after running this code once, confirm is not a function, but a boolean, in handler's function scope.

Change the local variable name to something else, like 'value' or 'confirmed'.

Upvotes: 0

Arun P Johny
Arun P Johny

Reputation: 388316

It is because of the local variable confirm.

Since you have declared a local variable with name confirm, when you use confirm() in your function it will have the value undefined as it is no longer referring to the global confirm function.

Just rename the variable and it should be fine.

$('#reset').click(function () {
    var value = confirm("This will reset everything, do you really want to continue?!");
    if (value == true) {
        alert();
    }
});

Upvotes: 3

Related Questions