Acutally, in most browsers,
function recieves one paramenter: event, which is the event which triggered the function.
You can use event.target inside
function to access the object that triggered the event. Internet Explorer (oh, bane of my existence!) has a slightly different implimentation. You have to use window.event to get the event and then element.srcElement to access the object. Here's an example:
- <ul id="test">
-
<li><span id="li1">Item 1</span></li>
-
<li><span id="li2">Item 2</span></li>
-
</ul>
-
<script>
-
function myfunction(e){
-
var obj = null;
-
if(!e){ e = window.event; }
-
obj = e.target ? e.target : e.srcElement;
-
alert(obj.id);
-
}
-
-
var list = document.getElementsByTagName("li");
-
for(var i in list){
-
list.onclick = myfunction; }
-
</script>
If that can't solve your issue, then use this syntax instead of "element.event = function;":
- element.event = Function("yourFunction("+params+");");
This works in IE and Firefox to my knowledge and should work in others as well (someone correct me if I'm wrong).[i]
Hello!
I need to attach a function with parameters to an event using the DOM standard, but i can't find a way to do it. I've used the following :
element.event = function;
But in this case function doesn't receive any parameter, and I don't know any other way to do it.
If anyone can give me hand on this, I'll really appreciate it.
Thanks!!!