Question

I have this js function that checks if OK or Cancel is pressed and returns false or true. What I am trying to do is that when is true to run the remaining js function in the order they appear but it isn't working; deleteList('$row->listID');updateList(); are never executed.

Javascript:

function deleteConfirm(){
    if (confirm("Are you sure you wanna delete that list?")){
            return true;
    }
    else{
            return false;
    }

}

HTML:

<a href="#" id="listID" onclick="return deleteConfirm();deleteList('$row->listID');updateList();">

What am I doing wrong?

Thanks in advance.

Was it helpful?

Solution

It's because you're returning on the first statement (so the others never run), instead only hop out on a cancel, like this:

onclick="if(deleteConfirm()) { deleteList('$row->listID'); updateList(); }"

You can also greatly simplify your function, like this:

function deleteConfirm(){
  return confirm("Are you sure you wanna delete that list?");
}

...or if possible go the unobtrusive route, using data- attributes to get the listID.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top