473,398 Members | 2,393 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

How to attach a function with parameters to an event using DOM

7
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!!!
Dec 5 '06 #1
1 1509
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:
Expand|Select|Wrap|Line Numbers
  1. <ul id="test">
  2.   <li><span id="li1">Item 1</span></li>
  3.   <li><span id="li2">Item 2</span></li>
  4. </ul>
  5. <script>
  6. function myfunction(e){
  7.   var obj = null;
  8.   if(!e){ e = window.event; }
  9.   obj = e.target ? e.target : e.srcElement;
  10.   alert(obj.id);
  11. }
  12.  
  13. var list = document.getElementsByTagName("li");
  14. for(var i in list){
  15.   list.onclick = myfunction; }
  16. </script>
If that can't solve your issue, then use this syntax instead of "element.event = function;":

Expand|Select|Wrap|Line Numbers
  1.  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!!!
Mar 17 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: gen_www | last post by:
I am running an applet in Internet Explorer using <Object> tag and trying to attach to it using Netbeans IDE. However Netbeans is not able to attach to the applet. However I can use Netbeans to...
4
by: bhatiaajay | last post by:
I am new to JavaScript objects. The object creates a button with no events attached. Later I want to attach an event to the button created by this object. The event attaches a function in the...
4
by: Julie J. | last post by:
When working w/ forms and controls, in 95% of the cases, the value of the control is used somewhere in the program (duh!). However, there doesn't seem to be an easy way to attach a value variable...
4
by: Child | last post by:
I am feeling frustrated and I am willing to bet its something stupid but I could really use some help. I have a class called anEvent. On my web form I have a button which creates a new anEvent...
26
by: johkar | last post by:
I need to cancel the link and execute a function onclick of all the links within the span tag which has a class of "container" assigned. There will be only one span tag with this class applied. ...
2
by: harel.moshe | last post by:
Hey everybody, I'm looking for a way to attach non-void events to a server control i wrote. Relevant code parts: the control: mycontrol : WebControl {
4
by: simon | last post by:
hi, I would like to separate my javascript completely from my xhtml. in the end there should be only <script type="text/javascript" src="javalib.js"></script> in the head-tag to my javascript....
1
by: acl123 | last post by:
Hi, I am trying to work out how to access the event object in firefox under two conditions - 1) Attaching event handlers in javascript, not html. 2) The event handler requires parameters in...
1
by: sindhu | last post by:
Hello, I am able to attach event to dynamically created element.as shown below. for(var i=0;i<10;i++) {for(var j=0;j<15;j++) { var div=document.createElement('div'); ...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.