0

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 ;
}
1
  • confirm() returns a boolean, so you can shorten your return statement by just returning the return value of the confirm. return confirm("Are you sure...");. Commented Jul 20, 2011 at 19:00

4 Answers 4

5

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

Sign up to request clarification or add additional context in comments.

5 Comments

The problem is these are built by PHP and thus both styles of quotes are already reserved. I have used PHP to convert the variable to a string but to no avail.
So escape the quote: echo 'onclick="confirmSubmit(\'abc\')"';
Ok but I need this to work with a variable... onClick="confirmSubmit('.$row['Cname'].') is true code
'...(\'' . $row['CName'] . '\')'
Thanks now the function runs but the variable passed to does not show in the dialog, the variable is definitely contains something because I echo it earlier.
1

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')"

Comments

1
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?

2 Comments

That was when it failed at first I was just trying out different things, it has since been removed :), can you see why category would be empty now I've followed cwolves advice? The function runs but there is a blank space in place of the variable category.
Can't you shorten and simplify the return to... return confirm("Are you sure you wish to DELETE" + category + " and all of its subcategories and photos?")==true;
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.