Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Javascript: Changing the Onclick attribute

  • 16-03-2004 1:07pm
    #1
    Registered Users, Registered Users 2 Posts: 394 ✭✭


    Not sure if this can be done:

    I want to change the onclick value of a link dynamically.

    Basically, I've got a form that doesn't have a submit button but just plain link to the checkout.

    If there are errors on the form, I disable the link (I've set the href to #, works) and change the onclick value of said link to alert("error message"); (doesn't work)

    At the moment, I'm setting it like this:
    document.getElementById("checkout_link").onclick = "whatever";

    and it looks like it sets the value fine when I do a display :

    document.getElementById("checkout_link").onclick

    but when I click the link, no go!


Comments

  • Registered Users, Registered Users 2 Posts: 394 ✭✭Mickah


    Just put javascript:alert('blah'); in for your href and forget about the onclick

    That should do it Mickah

    :)


  • Registered Users, Registered Users 2 Posts: 3,177 ✭✭✭oneweb


    While handy for usability (the user doesn't have to wait for the page to reload with an error message), it's important to note that Javascript can be disabled so you'll need to perform some server-side value checking too.

    It is indeed possible to change the value onclick.

    Put code along the lines of

    if (valuesok){
    document.location='linktocontinue'
    } else {
    alert('Following fields are invalid');
    }

    where valuesok is your form checking function.

    HTH

    It is what it's.



  • Registered Users, Registered Users 2 Posts: 394 ✭✭Mickah


    Yeah server side is covered, wouldn't use javascript at all if i could help it, but needs must.

    i tried setting the onclick to the same value as the href, it displayed as set, but just doesn't work when i click the link!

    odd


  • Registered Users, Registered Users 2 Posts: 3,177 ✭✭✭oneweb


    Wanna PM me a link so I can try it?

    It is what it's.



Advertisement