View Full Version : Can one pass arguments with dynamically assigned methods?

08-19-2005, 10:40 PM
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?

08-19-2005, 11:27 PM
An event object is passed by default. You can also access all arguments through the functions 'arguments' property.

08-20-2005, 12:20 AM
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.

08-20-2005, 01:11 AM
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.

08-20-2005, 01:19 AM
Example of what hemebond has mentioned:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
<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

<a href="/myPage.html" onclick="getHref(event);return false;">Click me to get the href of this link</a>

Hope this helps