0

I have a small piece of javascript to get a user to confirm things on my website. I have a link that I also ask them to confirm before proceeding. I've used the 'onclick' function with buttons no problem, but how do I use it on a link?

Heres my link:

echo"<a href=\"$urlHTML\">Delete</a>";

And heres my 'onclick':

onclick="return confirm('Please confirm you wish to edit these details')"/>

Any help would be great!

3 Answers 3

2

Ummm...

<?php ?>
<a href="<?php echo $urlHTML ?>" onclick="return confirm(blah blah blah)">Delete</a>
Sign up to request clarification or add additional context in comments.

2 Comments

@ marc b echo"<a href="<?echo $urlHTML?>" onclick="return confirm(Please Confirm You Wish to Add these Details)">Delete</a>";
Any chance you could give me an idea why im getting a 'unexpected '?'' error from this?
0

You can use onclick on an anchor tag the same way you'd use it on a button:

<a href="<?php echo $urlHTML; ?>" onclick="return confirm('Please confirm you wish to edit these details');">Delete</a>

However you'd be better off using an event handler:

<script type="text/javascript">
    function myClickHandler() {
        return confirm("Really?");
    }

    document.getElementById("my_link").onclick = myClickHandler;
</script>

<a id="my_link" href="<?php echo $urlHTML; ?>">Delete</a>

I recommend you also read up on jQuery. It'll save you a lot of time.

1 Comment

@ twaddington how would i use your 'anchor tag' method with it echo'd and with the '$html' link i posted. Im new to javascript. Ty
0

You might want to consider using addEventListener and attachEvent (IE) instead of using inline JavaScript.

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.