SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot jooles's Avatar
    Join Date
    Jun 2006
    Posts
    144
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    document.getElementById('id').ONCLICK ???

    Is there a way to change the action performed on a particular event of a particular element:

    document.getElementById('id').ONCLICK

    I've tried the above code and it doesn't seem to work. Or am I doing something wrong...

  2. #2
    SitePoint Addict ALL's Avatar
    Join Date
    Oct 2005
    Location
    South Dakota
    Posts
    215
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    yes you are... javascript is casesensitive... it needs to be:

    document.getElementById('id').onclick

    but remember... javascript's onclick is an actual event meaning just assigning it like this:

    document.getElementById('id').onclick="dosomething()";

    will NOT work if you want to do something like that use:

    document.getElementById('id').onclick=Function("dosomething()");

    -ALL
    Did I help you?
    You can repay me, support one of my projects (no money needed):
    JavaScript Wiki, Another Web Forum, Paranormal Site

  3. #3
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is no need to use the function constructor. It is not recommended to use it, since it basically needs to do an eval. Which is slow.

    Code:
    // Assign a function to deal with the onclick event:
    document.getElementById('id').onclick = doSomething;
    
    // or use an anonymous function:
    document.getElementById('id').onclick = function() {
        // do magic
    };


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •