George Reith
George Reith

Reputation: 13476

Javascript: Passing variable to function breaks function

I am using an onsubmit variable to ensure that the user really means to delete something, however as soon as I put a value in the parenthesis inside the onsubmit it no longer calls the confirm box.

Code:

onClick="confirmSubmit(abc)"

Doesn't work but the following:

onClick="confirmSubmit()"

Does work

Function:

    function confirmSubmit(category)
{
var category = category;
var agree=confirm("Are you sure you wish to DELETE" + category + " and all of its subcategories and photos?");
if (agree)
    return true ;
else
    return false ;
}

Upvotes: 0

Views: 2878

Answers (4)

MPS
MPS

Reputation: 31

function confirmSubmit(category)  
{  var category = category;  

And you've declared "category" twice! Once in the function header and then as a function variable in the next line! What for?

Upvotes: 1

binarious
binarious

Reputation: 4588

You're try to pass the variable abc (which does not exist) to the function. Do: onclick="return confirmSubmit('abc');"

Upvotes: 0

James Montagne
James Montagne

Reputation: 78770

onClick="confirmSubmit(abc)" is trying to pass the variable abc, if you intend to pass a string with the value "abc" then do this:

onClick="confirmSubmit('abc')"

Upvotes: 1

user578895
user578895

Reputation:

you need quotes around your abc:

onclick="confirmSubmit('abc')"

Without them you are trying to pass a variable, abc, which doesn't exist and triggers an error

Upvotes: 5

Related Questions