SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    May 2005
    Location
    Tauranga, New Zealand
    Posts
    358
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    onmouseover=handler with parameter

    Hi,

    I need to convert
    <div onmouseover="mousehand(event,'outerjob123');">

    to a javascript call, ie.

    x = document.createElement("div");
    id = 123;
    x.onmouseover = mousehand(event, 'outerjob' + id);



    However, the syntax doesn't seem quite right, can someone enlighten me?

    Jochen
    http://www.automatem.co.nz
    Websites, On-line Software and everything Internet
    Follow on Twitter | Connect on LinkedIn | Read on Posterous

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    x.onmouseover = mousehand(event, 'outerjob' + id);

    The right side of that assignment says to execute the function mousehand(). The '()' after a function name is the function execution operator. Since functions are replaced by their return value, you end up with:

    x.onmouseover = <value in mousehand return statement or undefined>;

    What you need to do is this:
    Code:
    x.onmouseover = wrapperFunc;
    
    function wrapperFunc()
    {
    	mousehand(event, "outer job" + id);
    }
    Notice how there is no function execution operator after wrapperFunc, so nothing gets executed at the time of the assignment. When 'x' finally calls wrapperFunc(), wrapperFunc() executes which in turn executes mousehand(). It's important to note that ultimately js calls the function assigned to onmousever in reaction to the user mousingover, so only js can send arguments to the function. The code can also be written more succinctly:
    Code:
    x.onmouseover = function(){ mousehand(event, "outer job" + id) };
    Last edited by 7stud; Nov 1, 2005 at 00:03.

  3. #3
    SitePoint Addict
    Join Date
    May 2005
    Location
    Tauranga, New Zealand
    Posts
    358
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    First of all: You guys rock!

    Everytime I have asked a question on Javascript in this forum, I have received a large number of to-the-point replies which solved my problem. The answers are also very indepth and short at the same time and have allowed me to grasp the concepts of Javascript much better.

    Thank you very much!

    In regards to the question, the solution works fine of course.

    Jochen
    http://www.automatem.co.nz
    Websites, On-line Software and everything Internet
    Follow on Twitter | Connect on LinkedIn | Read on Posterous


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
  •