473,796 Members | 2,625 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("namec lick(" + strand + ");");
inp.onkeyup = new Function("namec hange(" + 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(stra nd)
{
var tbody = document.getEle mentById("S" + strand +
"Names").getEle mentsByTagName( "TBODY")[0];
var numnames = document.getEle mentById("S" + strand +
"NumNames") ;
numnames.value = parseInt(numnam es.value)+ 1;

var row = document.create Element("TR");

td = document.create Element("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.create Element("TD");
td1.id = "S" + strand + "NameRow" + numnames.value;
var inp = document.create Element("INPUT" );

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

if(inp.attachEv ent)
{
//Do IE Specific
inp.attachEvent ('onkeyup', makeEventFunc1( strand));
inp.attachEvent ('onchange', makeEventFunc2( strand));
}
else
{
inp.onchange = new Function("namec lick(" + strand + ");");
inp.onkeyup = new Function("namec hange(" + strand + ");");
}

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

tbody.appendChi ld(row);

}

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

}

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

}

Nov 19 '06 #1
5 12596
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(stra nd);});
inp.attachEvent ('onkeyup',
function(event) {namechange(str and);});

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(stra nd);});
inp.attachEvent ('onkeyup',
function(event) {namechange(str and);});

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 addEventListene r only if you want to add multiple
handlers for the same event. It is much more reliable to use your "non
IE" method:

element.eventNa me = functionName /or/ functionExpress ion;

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.addEventLis tener){
el.addEventList ener(evt, fn, capture);
} else if (el.attachEvent ){
el.attachEvent( 'on'+evt, fn);
}
}

function nameClick(){ale rt('nameClick') ;}

function nameChange(){al ert('nameChange ');}

</script>

<input type="text" id="inp0">
<input type="button" value="addEvent 0" onclick="
var el = document.getEle mentById('inp0' );
addEvent0(el, 'keyup', nameClick);
addEvent0(el, 'change', nameChange);
">
<input type="button" value="addEvent 1" onclick="
var el = document.getEle mentById('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("namec hange(" + strand + ");");
inp.onchange = new Function("namec lick(" + 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(stra nd);});
inp.attachEvent ('onkeyup',
function(event) {namechange(str and);});

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("namec hange(" + strand + ");");
inp.onchange = new Function("namec lick(" + 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(stra nd);});
inp.attachEvent ('onkeyup',
function(event) {namechange(str and);});

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
3334
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 is possible to Add and Delete rows - Some cells have special attributes (readonly and events) Here's a snippet of the code:
33
2871
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" onclick="show(this)">111111</li>
2
5206
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? Thanks. <HTML> <HEAD>
1
1670
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 literal object is not defined. function initialize() { insertScriptTags("http://dante.acomp.usf.edu/CommunicationSimulator/prototype.js"); insertScriptTags("http://dante.acomp.usf.edu/CommunicationSimulator/scriptaculous.js");...
5
2255
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 created a few functions to aid in making this process work semi-cross-browser. (I develop in FF, but everyone who uses it will be using IE6.) function attachEvent(_obj, _evt, _fnc) { _evt = _evt.toLowerCase(); if (_obj.attachEvent) {
4
1745
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 the AREAs of my image map. Here is an abbreviated version of my code: ------------------------
21
29827
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 prodcut anme have used ajax to avoid refreshing of page this works fine 2.now i have created one row with checkbox|select box|text|text|text|text| where in the select box values are fetched from table here also i have used ajax for getting the m_name...
0
10239
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10019
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9057
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7555
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5447
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5579
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4122
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2928
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.