SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot imagize's Avatar
    Join Date
    Oct 2004
    Location
    Australia
    Posts
    197
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unobtrusive Javascript Function Parameters

    Ok this has been bugging me for awhile now. I am able to create unobtrusive javascript using load listeners etc. But I am unable to pass parameters to functions this way. Like in an onload function you might have something like

    Code:
    document.getElementById("test").onclick = showMessage('hello');
    Let's just say the showMessage function just takes what ever is in the parameter and uses alert();.

    The point is the above code doesn't work at all, it seems at the moment if my functions require a special parameter I must manually insert into the html which i am trying to avoid.

    I tried this but no luck either.

    Code:
    document.getElementById("test").onclick = function() 
    {
       showMessage('hello'); 
    };
    Any help appreciated.

  2. #2
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sounds like you need to run the second version from a window.onload event handler. This is the simplest way. (I don't recommend using something this simple for normal pages.)

    Try this. If it still doesn't work, we'll need to see more of your code.
    Code:
    window.onload = function() {
      document.getElementById("test").onclick = function() 
      {
        showMessage('hello'); 
      }
    }
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.


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
  •