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

Javascript: Changing the Onclick attribute

Options
  • 16-03-2004 2:07pm
    #1
    Registered Users 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 Posts: 394 ✭✭Mickah


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

    That should do it Mickah

    :)


  • Registered Users Posts: 3,147 ✭✭✭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 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 Posts: 3,147 ✭✭✭oneweb


    Wanna PM me a link so I can try it?

    It is what it's.



Advertisement