Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    May 2004
    Location
    Minneapolis, MN, USA
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Can one pass arguments with dynamically assigned methods?

    I know that assigning functions to events requires you to supply a reference to the function itself, not a call of the function. So is there any way one could pass arguments, or would one have to code a script such that it could perhaps detect differences in its environment instead?

  • #2
    Senior Coder
    Join Date
    Jul 2004
    Location
    New Zealand
    Posts
    1,315
    Thanks
    0
    Thanked 2 Times in 2 Posts
    An event object is passed by default. You can also access all arguments through the functions 'arguments' property.
    Forget style. Code to semantics. Seperate style from structure, and structure from behaviour.
    I code to specs, and test only in Firefox (unless stated otherwise).

  • #3
    Regular Coder
    Join Date
    May 2004
    Location
    Minneapolis, MN, USA
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Okay, and what does the event object do for me in this circumstance?

    Also, that point about the arguments array is great except that it seems to forget the whole premise of my thread, being that one can't pass arguments when assigning functions to events.

    So I'm not really sure what you're trying to say.

  • #4
    Senior Coder
    Join Date
    Jul 2004
    Location
    New Zealand
    Posts
    1,315
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Well, without having any idea what you're trying to achieve, the event object will give you access to the element where you could have certain attributes. Or you could possibly use an anomymous function as the event handler, the contents of which calls another function with arguments.
    Forget style. Code to semantics. Seperate style from structure, and structure from behaviour.
    I code to specs, and test only in Firefox (unless stated otherwise).

  • #5
    Regular Coder martin_narg's Avatar
    Join Date
    Jul 2002
    Location
    Chamonix, France
    Posts
    600
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Example of what hemebond has mentioned:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitled Document</title>
    <script type="text/javascript">
    function getHref(e) {
    	var e = e || window.event; // grab event object - try gecko then IE
    	var el = e.target || e.srcElement; // get element clicked - try gecko then IE
    	alert(el.getAttribute("href")); // alert the link href
    }
    </script>
    </head>
    
    <body>
    <a href="/myPage.html" onclick="getHref(event);return false;">Click me to get the href of this link</a>
    </body>
    </html>
    Hope this helps

    m_n
    "Cos it's strange isn't it. You stand in the middle of a library and go 'Aaaaaaaaaaaaaaaaggggggghhhhhhh!'
    and everybody just stares at you. But you do the same in an aeroplane, and everybody joins in."
    -Tommy Cooper


  •  

    Posting Permissions

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