472,806 Members | 1,801 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

How to dynamically assign event handler functs w/ parameters?

How do you dynamically assign a function to an element's event with
specific parameters?

I know that the code is different for MSIE and Mozilla, and need to know
how to do this for both.

I have the following event handler functions defined and need to assign
them to table elements (<TD>) created dynamically in another function.

function handleMouseOver(elt, cssClass, strURL)
{
....
}

handleMouseOut(elt, cssClass)
{
....
}

handleMouseDown(elt, cssClass)
{
....
}

handleMouseUp(elt, cssClass, strURL)
{
....
}
The code frag below is where they need to be assigned.

Thanks,
N. Demos

Example (Code Fragment):
-------------------------

for(i = 0; i < 5; i++){
// Construct 'TR' Node
var eltNewRow = document.createElement('TR');
eltNewRow.id = (strMenuID + '-SUBMENU_BUTTON' + i);

// Construct 'TD' Button
var eltButton = document.createElement('TD');
eltButton.appendChild(document.createTextNode('Sub Button ' + (i +
1)));
eltButton.className = 'subNavNormButton';

var strTargetURL = arrayURLList[i];

// ***** Assign Mouse Event Handlers *****
eltButton.onmouseover = handleMouseOver(this, 'subNavHoverButton',
strTargetURL);
eltButton.onmouseout = handleMouseOut(this, 'subNavNormButton');
eltButton.onmousedown = handleMouseDown(this, 'subNavPressButton');
eltButton.onmouseup = handleMouseUp(this, 'subNavHoverButton',
strTargetURL);

// Insert Element Into Tree
eltNewRow.appendChild(eltButton);

if(eltInsertionRow && parentMenu){
parentMenu.insertBefore(eltNewRow, eltInsertionRow);
}
else if(parentMenu){
parentMenu.appendChild(eltNewRow);
}
}

--
Change "seven" to a digit to email me.
Jul 23 '05 #1
3 12404
N. Demos wrote:
How do you dynamically assign a function to an element's event with
specific parameters?

[...]

Read this thread:

<URL:http://groups-beta.google.com/group/comp.lang.javascript/browse_thread/thread/219168d56980e199/b9f7823d34a7001b?q=%22RobG%22&_done=%2Fgroup%2Fcom p.lang.javascript%2Fsearch%3Fq%3D%22RobG%22%26star t%3D60%26scoring%3Dd%26&_doneTitle=Back+to+Search& &d#b9f7823d34a7001b>

Or search for "Dynamically adding onclick to element".

--
Rob.
Jul 23 '05 #2
RobG wrote:
N. Demos wrote:
How do you dynamically assign a function to an element's event with
specific parameters?

[...]

Read this thread:

<URL:http://groups-beta.google.com/group/comp.lang.javascript/browse_thread/thread/219168d56980e199/b9f7823d34a7001b?q=%22RobG%22&_done=%2Fgroup%2Fcom p.lang.javascript%2Fsearch%3Fq%3D%22RobG%22%26star t%3D60%26scoring%3Dd%26&_doneTitle=Back+to+Search& &d#b9f7823d34a7001b>
Or search for "Dynamically adding onclick to element".


Here's a little more help, I've implemented the advice given by
Richard Cornford in response to the above post. I was a little
daunted by it when I first read it, but I think I have a better
understanding now.

Each event just prints out its parameters, the internal code
can be replaced with whatever you like.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Dynamically add onclick with parameters</title>
<meta http-equiv="Content-Type"
content="text/html; charset=ISO-8859-1">
<script type="text/javascript">
function handleMouseOver(elt, cssClass, strURL) {
var msg = 'handleMouseOver: ' + elt.nodeName;
'handleMouseOver: ' + elt.nodeName
for (var i=0; i<arguments.length; i++) {
msg += '<br> Parameter ' + i
+ ': ' + arguments[i];
}
document.getElementById('msgBox').innerHTML = msg;
}

function handleMouseOut(elt, cssClass) {
var msg = 'handleMouseOut: ' + elt.nodeName;
'handleMouseOut: ' + elt.nodeName + ' : '
for (var i=0; i<arguments.length; i++) {
msg += '<br> Parameter ' + i
+ ': ' + arguments[i];
}
document.getElementById('msgBox').innerHTML = msg;
}

function handleMouseDown(elt, cssClass) {
var msg = 'handleMouseDown: ' + elt.nodeName;
for (var i=0; i<arguments.length; i++) {
msg += '<br> Parameter ' + i
+ ': ' + arguments[i];
}
document.getElementById('msgBox').innerHTML = msg;
}

function handleMouseUp(elt, cssClass, strURL){
var msg = 'handleMouseUp: ' + elt.nodeName;
'handleMouseUp: ' + elt.nodeName + ' : '
for (var i=0; i<arguments.length; i++) {
msg += '<br> Parameter ' + i
+ ': ' + arguments[i];
}
document.getElementById('msgBox').innerHTML = msg;
}

function addClicks(id){
var el = document.getElementById(id);
// ***** Assign Mouse Event Handlers *****

var strTargetURL = 'strTargetURL parameter';

el.onmouseover = function (){
handleMouseOver(this, 'subNavHoverButton', strTargetURL)
};
el.onmouseout = function (){
handleMouseOut(this, 'subNavNormButton')
};
el.onmousedown = function (){
handleMouseDown(this, 'subNavPressButton')
};
el.onmouseup = function (){
handleMouseUp(this, 'subNavHoverButton', strTargetURL)
};
}

</script>

</head>

<body onload="addClicks('targetBtn');">

<button id="targetBtn">A button</button>
<br>
<span id="msgBox"></span>

</body>
</html>


--
Rob
Jul 23 '05 #3
RobG wrote:
N. Demos wrote:
How do you dynamically assign a function to an element's event with
specific parameters?

[...]

Read this thread:

<URL:http://groups-beta.google.com/group/comp.lang.javascript/browse_thread/thread/219168d56980e199/b9f7823d34a7001b?q=%22RobG%22&_done=%2Fgroup%2Fcom p.lang.javascript%2Fsearch%3Fq%3D%22RobG%22%26star t%3D60%26scoring%3Dd%26&_doneTitle=Back+to+Search& &d#b9f7823d34a7001b>
Or search for "Dynamically adding onclick to element".


RobG,
Thank you for your time and the info. This did help.
Unfortunately, the form of event assignment referenced in the post above
isn't working the way I need it to.

Example:
--------
var listUrls = new Array(3);

listUrls[0] = new String('Page1.html');
listUrls[1] = new String('Page2.html');
listUrls[2] = new String('Page3.html');

var strCssClass = 'buttonNorm';

for(i = 0; i < listUrls.length; i++){
// Create Element Code here ...
objElement = document.CreateElement('TD');
objElement.onmouseover = function(e) {handleMouseOver(e,
strCssClass, listUrls[i]);}
//etc...
}

What appears to be happening when the event handler is called for
each/any element it was assigned, it is called with the last parameters
it was passed in the loop.

In this case, it's called handleMouseOver(e, 'buttonNorm', 'Page3.html')
I do not fully understand why this is occuring. I appears as if there is
only one function being assigned (to all the Elements) within the loop
and, at execution, is utilizing whatever the last set of parameters it
was passed.

However, I have found a solution that does work.

Working Soln:
--------------
var listUrls = new Array(3);

listUrls[0] = new String('Page1.html');
listUrls[1] = new String('Page2.html');
listUrls[2] = new String('Page3.html');

var strCssClass = 'buttonNorm';

for(i = 0; i < listUrls.length; i++){
// Create Element Code here ...
objElement = document.CreateElement('TD');

var strFunctMO = 'handleMouseOver(e, "' + strCssClass + '", "' +
listUrls[i] + '");';

objElement.onmouseover = new Function('e', strFunctMO);
// Other event handler assigns here ...
}

--
Change "seven" to a digit to email me.
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Eric | last post by:
How can I dynamically assign an event to an element? I have tried : (myelement is a text input) document.getElementById('myelement').onKeyUp = "myfnc(param1,param2,param3)"; ...
2
by: Adi | last post by:
Okay, this issue has been annoying me for a little while now. I need it to work on Mozilla 1.6 & IE 6 This is what I would like to be able to do: var row = document.createElement("TR"); var...
8
by: cool2005 | last post by:
I tried to dynamically add/clone a <tr> from an existing <tr> to a <table> but the problem is that I need to have a "onclick" event handler in the <tr>. I have tried following A. approach...
2
by: Linda | last post by:
Hi, How do I dynamically add linkbuttons and wire them to same event? I am able to add linkbuttons but they do not fire the event. Does anybody have a working sample? Many thanks, Linda
3
by: r_o | last post by:
hi all, i'm trying to dynamically assign an event handler for an onmouseover event on a <Li> element. i need to pass the event as a parameter to the handler as follows <script...
7
by: Ron Goral | last post by:
Hello I am new to creating objects in javascript, so please no flames about my coding style. =) I am trying to create an object that will represent a "div" element as a menu. I have written...
12
by: vbnewbie | last post by:
I am having problems accessing properties of dynamically generated objects in VB2005. Can someone please help? In a nutshell: My app creates an equal number of checkboxes and labels that share the...
4
by: Craig Buchanan | last post by:
I dynamically add data-bound templates to a gridview in my ascx control. while this works correctly when the gridview is databound to the datatable, i'm having issues on postback. i would like...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?

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.