document.getElementById('myID').onclick ?

Posted on 2005-04-05
Last Modified: 2012-08-14
This works:
<a id="myLink">link test</a>

Is it possible to do more or less the same thing with onclick? If so, how? I can't get it to work.

<a href="javascript:void(0)" id="myLink">link test</a>


Question by:GessWurker
    LVL 7

    Expert Comment

    LVL 3

    Accepted Solution

    function doSomethingClever()
    return false;


    Author Comment

    hackman_3vilGuy: Yours worked for me. Thanks!

    Can't use any quotes though, right?

    document.getElementById('myLink').onclick=doSomethingClever;  <--this works

    document.getElementById('myLink').onclick="doSomethingClever";<--this doesn't work
    document.getElementById('myLink').onclick="doSomethingClever()";<-- this doesn't work
    document.getElementById('myLink').onclick=doSomethingClever();<--this doesn't work either

    LVL 3

    Expert Comment

    Note that in the registration of an event handler you do not use brackets (). The onclick method expects to be assigned an entire function. If you’d do

    element.onclick = doSomething();

    the function would be executed and its result would be registered to onclick. This is not what we want, we want the function to be executed when the event takes place. In addition the function has usually been written to expect an event and if we’d execute it without any such context it would get terribly confused and produce JavaScript errors.

    Instead we copy the function doSomething() to the event handler in its entirety. We do not execute it yet, that should only happen when the event actually takes place.

    see for more info

    Author Comment

    Thanks for the solution and the great explanation!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    "That which we persist in doing becomes easier, not that the task itself has become easier, but that our ability to perform it has improved." Ralph Waldo Emerson Introduction: One of the wonderful things about the web is that it makes it s…
    In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now