473,406 Members | 2,698 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,406 software developers and data experts.

Problem with dynamically adding event handlers in IE - setAttribute and attachEvent

J
I am having problems dynamically adding more than one event handler to
an input. I have tried the Javascript included at the bottom. The
lines

inp.attachEvent('onkeyup', makeEventFunc1(strand));
inp.attachEvent('onchange', makeEventFunc2(strand));
individually work in IE, but when used together, only the bottom one
remains active.

I have also tried
inp.onchange = new Function("nameclick(" + strand + ");");
inp.onkeyup = new Function("namechange(" + strand + ");");

but the code has problems in IE6. If I include one of the lines, it
will work in IE6. If I include both lines, neither event handler
works. All of the code works fine in Mozilla. Is there another way to
dynamically add two event handlers to a control that will work in IE?

Please help.

-Jason

function addNameRow(strand)
{
var tbody = document.getElementById("S" + strand +
"Names").getElementsByTagName("TBODY")[0];
var numnames = document.getElementById("S" + strand +
"NumNames");
numnames.value = parseInt(numnames.value)+ 1;

var row = document.createElement("TR");

td = document.createElement("TD");
td.className = 'sitetext';
td.innerHTML = "Enter Name #" + numnames.value + " for this
Strand (Optional)";
row.appendChild(td);

var n = "S" + strand + "Name" + numnames.value;
td1 = document.createElement("TD");
td1.id = "S" + strand + "NameRow" + numnames.value;
var inp = document.createElement("INPUT");

inp.size = 16;
inp.name=n;
inp.id=n;

if(inp.attachEvent)
{
//Do IE Specific
inp.attachEvent('onkeyup', makeEventFunc1(strand));
inp.attachEvent('onchange', makeEventFunc2(strand));
}
else
{
inp.onchange = new Function("nameclick(" + strand + ");");
inp.onkeyup = new Function("namechange(" + strand + ");");
}

td1.appendChild(inp);
row.appendChild(td1);

tbody.appendChild(row);

}

function makeEventFunc1( param1 )
{
return function()
{
nameclick(param1);
}

}

function makeEventFunc2( param1 )
{
return function()
{
namechange(param1);
}

}

Nov 19 '06 #1
5 12532
J wrote:
I am having problems dynamically adding more than one event handler to
an input. I have tried the Javascript included at the bottom. The
lines

inp.attachEvent('onkeyup', makeEventFunc1(strand));
makeEventFunc1(strand) is a function invocation because of the parens
after the function name. The second argument to attachEvent is a
function to be used as the event handler not a function invocation.
When the event handler makeEventFunc1 is called automatically by IE at
the appropriate time, the event handler function will be passed one
argument: the event object.

If you want to pass an argument to the event handler you can use a
closure. I'm not sure what you are doing exactly but something like
this would pass the string "foo" to makeEventFunc1.

strand = "foo";
inp.attachEvent('onkeyup', function(event){makeEventFunc1(strand);});

Peter

Nov 19 '06 #2
J
The method you suggested is correct, but still doesn't solve my
problem. If I put the following lines to attach the two events:

inp.attachEvent('onchange',
function(event){nameclick(strand);});
inp.attachEvent('onkeyup',
function(event){namechange(strand);});

Only the onkeyup event is active (If I put the "onchange" event
second, it will be active.) I can't get both to be active at the same
time using these calls for IE6. Again, mozilla works fine with the
same code. It's as though the IE model only has room to attach one
event. Anyone else seen this issue?

Peter Michaux wrote:
J wrote:
I am having problems dynamically adding more than one event handler to
an input. I have tried the Javascript included at the bottom. The
lines

inp.attachEvent('onkeyup', makeEventFunc1(strand));

makeEventFunc1(strand) is a function invocation because of the parens
after the function name. The second argument to attachEvent is a
function to be used as the event handler not a function invocation.
When the event handler makeEventFunc1 is called automatically by IE at
the appropriate time, the event handler function will be passed one
argument: the event object.

If you want to pass an argument to the event handler you can use a
closure. I'm not sure what you are doing exactly but something like
this would pass the string "foo" to makeEventFunc1.

strand = "foo";
inp.attachEvent('onkeyup', function(event){makeEventFunc1(strand);});

Peter
Nov 20 '06 #3

J wrote:
The method you suggested is correct, but still doesn't solve my
Please don't top post, reply below trimmed quotes.

problem. If I put the following lines to attach the two events:

inp.attachEvent('onchange',
function(event){nameclick(strand);});
inp.attachEvent('onkeyup',
function(event){namechange(strand);});

Only the onkeyup event is active (If I put the "onchange" event
second, it will be active.) I can't get both to be active at the same
time using these calls for IE6. Again, mozilla works fine with the
same code. It's as though the IE model only has room to attach one
event. Anyone else seen this issue?
Remove the "IE specific" code completely, it does nothing useful. Use
attachEvent and addEventListener only if you want to add multiple
handlers for the same event. It is much more reliable to use your "non
IE" method:

element.eventName = functionName /or/ functionExpression;

Your issue is likely in regard to a conflict between onchange and
onkeyup. The following example may help - note that in IE you need to
click outside the input to make the onchange handler fire, whereas in
Firefox, the onchange alert causes the input to lose focus and hence
onchange to fire.

<script type="text/javascript">

// Single handler method
function addEvent0(el, evt, fn){
el['on'+evt] = fn;
}

// Multiple handler method
function addEvent1(el, evt, fn, capture){
if (el.addEventListener){
el.addEventListener(evt, fn, capture);
} else if (el.attachEvent){
el.attachEvent('on'+evt, fn);
}
}

function nameClick(){alert('nameClick');}

function nameChange(){alert('nameChange');}

</script>

<input type="text" id="inp0">
<input type="button" value="addEvent0" onclick="
var el = document.getElementById('inp0');
addEvent0(el, 'keyup', nameClick);
addEvent0(el, 'change', nameChange);
">
<input type="button" value="addEvent1" onclick="
var el = document.getElementById('inp0');
addEvent1(el, 'keyup', nameClick, false);
addEvent1(el, 'change', nameChange, false);
">
--
Rob

Nov 20 '06 #4
J
Slight correction to the previous post. If the line with "onkeyup" is
present, that event is active and works fine. while the "onchange"
doesnt fire regardless of the order the lines are writtern. If the
"onchange" line is alone, it works fine. The same is true if
following syntax is used:

inp.onkeyup = new Function("namechange(" + strand + ");");
inp.onchange = new Function("nameclick(" + strand + ");");
J wrote:
The method you suggested is correct, but still doesn't solve my
problem. If I put the following lines to attach the two events:

inp.attachEvent('onchange',
function(event){nameclick(strand);});
inp.attachEvent('onkeyup',
function(event){namechange(strand);});

Only the onkeyup event is active (If I put the "onchange" event
second, it will be active.) I can't get both to be active at the same
time using these calls for IE6. Again, mozilla works fine with the
same code. It's as though the IE model only has room to attach one
event. Anyone else seen this issue?

Peter Michaux wrote:
J wrote:
I am having problems dynamically adding more than one event handler to
an input. I have tried the Javascript included at the bottom. The
lines
>
inp.attachEvent('onkeyup', makeEventFunc1(strand));
makeEventFunc1(strand) is a function invocation because of the parens
after the function name. The second argument to attachEvent is a
function to be used as the event handler not a function invocation.
When the event handler makeEventFunc1 is called automatically by IE at
the appropriate time, the event handler function will be passed one
argument: the event object.

If you want to pass an argument to the event handler you can use a
closure. I'm not sure what you are doing exactly but something like
this would pass the string "foo" to makeEventFunc1.

strand = "foo";
inp.attachEvent('onkeyup', function(event){makeEventFunc1(strand);});

Peter
Nov 20 '06 #5
J
I solved the problem by changing "onchange" to "onblur".

J wrote:
Slight correction to the previous post. If the line with "onkeyup" is
present, that event is active and works fine. while the "onchange"
doesnt fire regardless of the order the lines are writtern. If the
"onchange" line is alone, it works fine. The same is true if
following syntax is used:

inp.onkeyup = new Function("namechange(" + strand + ");");
inp.onchange = new Function("nameclick(" + strand + ");");
J wrote:
The method you suggested is correct, but still doesn't solve my
problem. If I put the following lines to attach the two events:

inp.attachEvent('onchange',
function(event){nameclick(strand);});
inp.attachEvent('onkeyup',
function(event){namechange(strand);});

Only the onkeyup event is active (If I put the "onchange" event
second, it will be active.) I can't get both to be active at the same
time using these calls for IE6. Again, mozilla works fine with the
same code. It's as though the IE model only has room to attach one
event. Anyone else seen this issue?

Peter Michaux wrote:
J wrote:
I am having problems dynamically adding more than one event handler to
an input. I have tried the Javascript included at the bottom. The
lines

inp.attachEvent('onkeyup', makeEventFunc1(strand));
>
makeEventFunc1(strand) is a function invocation because of the parens
after the function name. The second argument to attachEvent is a
function to be used as the event handler not a function invocation.
When the event handler makeEventFunc1 is called automatically by IE at
the appropriate time, the event handler function will be passed one
argument: the event object.
>
If you want to pass an argument to the event handler you can use a
closure. I'm not sure what you are doing exactly but something like
this would pass the string "foo" to makeEventFunc1.
>
strand = "foo";
inp.attachEvent('onkeyup', function(event){makeEventFunc1(strand);});
>
Peter
Nov 20 '06 #6

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

Similar topics

6
by: Thomas | last post by:
Hi, I'm having a problem with the dynamically created inputfields in Internet Explorer. The situation is the following: - I have a dynamically created table with a textbox in each Cell. - It...
33
by: abs | last post by:
Hi all. My list: <ul> <li id="a" onclick="show(this)">Aaaaaaaa</li> <li id="b" onclick="show(this)">Bbbbbbbb</li> <li id="c" onclick="show(this)">Cccccccc <ul> <li id="d"...
2
by: jiayanxiang | last post by:
Hi: I used the following code to dynamically add a control and I tried to use attachevent to add a onClick event to the control. It doesn't seem to do anything. Any idea what might be wrong? ...
1
by: James Black | last post by:
You can see the program at: http://dante.acomp.usf.edu/CommunicationSimulator/index.php I am trying to dynamically add script tags to my webpage, but I get an error that the first javascript...
5
by: Chris Lieb | last post by:
I am trying to add an event listener to the keyup event for some text inputs that I am creating dynamically. I have no problems getting it to work in Firefox, but IE just ignores them. I have...
4
by: Andrew Ip | last post by:
Hi everyone, I'm trying to dynamically create an image map for a particular image on my website, and I'm running into an issue where I try to register the "mouseover" and "mouseout" events for...
21
by: Leena P | last post by:
i want to basically take some information for the product and let the user enter the the material required to make this product 1.first page test.php which takes product code and displays...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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,...

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.