Search code examples
javascriptdomhref

How to execute href based on javascript confirm() output


i have a link as follows:

<a onclick="deleteCourseLink()" href=examcont?action=RemoveCourseLinkAction></a>

and the deleteCourseLink() method is as follows:

   function deleteCourseLink(){


              if(!confirm('${textResources['ListCourseLinks.DeleteMessage']}')){
                    return false;
                }
          }

i want to execute the href only if the return value of the method is true, i.e when the return value is false(when cancel is pressed) nothing should happen, any idea about how to do that ?


Solution

  • Are you saying that the question does appear, but then the link doesn't get cancelled even if they press Cancel?

    If so, just change the link to:

    <a onclick="return deleteCourseLink()" href=examcont?action=RemoveCourseLinkAction></a>
    

    (added the word return).

    The rest of your code is correct (assuming ${textResources['ListCourseLinks.DeleteMessage']} is a server-side variable?).

    Although you could choose to simplify it to:

    function deleteCourseLink(){
        return confirm('${textResources['ListCourseLinks.DeleteMessage']}');
    }