Search code examples
javascriptonclickconfirm

onclick return confirm is not working


Overview:

I am using an anchor link here in my code to navigate to my delete script but before running the script I want javascript to come up with a popup that says are you sure to delete, and thus on confirmation goes and delete the data by running the script I wrote in delete_subject.php.

Problem:

When I click on the anchor tag, it doesn't bring the popup but runs the script directly and deletes the data. So, why does the JavaScript not run?

Code:

    <a href="delete_subject.php?
    subject=<?php echo $current_subject["id"];?>"
    onclick="return confirm("Are you sure?");">Delete subject</a>

Solution

  • You're nesting your quotes incorrectly.

    Try this instead:

    <a href="delete_subject.php?
    subject=<?php echo $current_subject["id"];?>"
    onclick="return confirm('Are you sure?');">Delete subject</a>
    

    When inside double quotes, you'll have to use single quotes to work with strings in JavaScript.