473,386 Members | 1,745 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,386 software developers and data experts.

Events in dynamically created DIV

Hi there,
I'd like to create two DIVs, one inside the other and capture the
click event of the inner one. Seems it should be trivial to do but I
somehow can't manage. Below here is the code I'm using [I know that it
does not work in IE but FireFox & Opera are enough for now].
Clicking on the 'Inner' div brings up the container's event with the
expected phase (3 = bubbling) but the Inner event is not fired at all.
I feel that ther is something obvious missing but I can't figure out
what.
I would be grateful for any help and/or pointers. Thanks.

START CODE:
<html>
<head>
</head>
<body>
<script>

var containerDiv = document.createElement('div');
containerDiv.style.border = '1px black solid';
containerDiv.innerHTML = 'Container before';

var theDiv = document.createElement('div');
theDiv.innerHTML = 'Inner';
theDiv.style.border = '1px red solid';
theDiv.addEventListener('click', function(e) {alert( 'Inner '+
e.eventPhase )}, false);

containerDiv.appendChild( theDiv );

containerDiv.innerHTML += 'Container after';
containerDiv.addEventListener('click', function(e) {alert(
'Container '+ e.eventPhase )}, false);
document.body.appendChild( containerDiv );

</script>
</body>
</html>

Dec 17 '06 #1
3 1619
ASM
zp*******@yahoo.co.uk a écrit :
Hi there,
I'd like to create two DIVs, one inside the other and capture the
click event of the inner one. Seems it should be trivial to do but I
somehow can't manage. Below here is the code I'm using [I know that it
does not work in IE but FireFox & Opera are enough for now].
Clicking on the 'Inner' div brings up the container's event with the
expected phase (3 = bubbling) but the Inner event is not fired at all.
Do not understand what you want
I get clicking in :
- main div (black bordered) : Container 2
- inserted div (red bordered) : Container 3

FF 2 on Mac

--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Stephane Moriaux and his (less) old Mac already out of date
Dec 17 '06 #2
zp*******@yahoo.co.uk wrote:

Hi,
I'd like to create two DIVs, one inside the other and capture the
click event of the inner one. Seems it should be trivial to do but I
somehow can't manage.
You have bumped into one particular subtlety of innerHTML. To have your
code work as you expect, simply use DOM methods, not innerHTML.
containerDiv.innerHTML += 'Container after';
This statement first retrieves a serialized string of the HTML within
the 'containerDiv' element, then append the string 'Container after' to
this string and eventually evaluates the whole string back into HTML
elements, recreating new objects.

If your case, your handlers are not written in the first innerHTML
output, and therefore are not recreated when your inner div element is
recreated with the innerHTML final evaluation. Use createTextNode
instead of innerHTML.

The real interesting issue lies with what innerHTML includes in its
string representation (or excludes from it). Basically, there are many
things that it cannot or may not represent, following are three (non
exhaustive) examples that spring to mind.

[1] _Events_handlers_ are a particular area of the problem, the
possibility to add thousands of listeners to only one element,
and the difficulty to define an appropriate 'anonymous' attribute
name, probably makes them relevant not to output.

[2] _Attributes_pointing_to_objects_, and not primitive values, cannot
be represented while the execution is still active. The
serialization process implies to keep objects' identities, which
cannot be done if, in some way, the object can still be updated
or deleted. An 'exception' could be these native event attributes,
for which some additional logic is run at the document load.

[3] _Expando_properties_, i.e. properties not belonging to the set of
authorized attributes, may be represented (IE) or not (Mozilla and
Opera). Still, adding thousands of them could make IE encounter its
limit...
Kind regards,
Elegie.
Dec 17 '06 #3
Me

Elegie wrote:
zp*******@yahoo.co.uk wrote:

Hi,
I'd like to create two DIVs, one inside the other and capture the
click event of the inner one. Seems it should be trivial to do but I
somehow can't manage.

You have bumped into one particular subtlety of innerHTML. To have your
code work as you expect, simply use DOM methods, not innerHTML.
Thank you very much for the explanation.
For the records, here is the code that works:

var containerDiv = document.createElement('div');
containerDiv.style.border = '1px black solid';
containerDiv.appendChild( document.createTextNode('Container
before') );

var theDiv = document.createElement('div');
theDiv.style.border = '1px red solid';
theDiv.appendChild( document.createTextNode('Inner') );
theDiv.addEventListener('click', function(e) {alert( 'Inner '+
e.eventPhase )}, false);

containerDiv.appendChild( theDiv );

containerDiv.appendChild( document.createTextNode('Container
after') );

containerDiv.addEventListener('click', function(e) {alert(
'Container '+ e.eventPhase )}, false);
document.body.appendChild( containerDiv );

Dec 18 '06 #4

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...
4
by: blue | last post by:
I have a drop-down list, a radio button list and a submit button. I'm adding these controls to a table and I'm adding the table to a Placeholder. I'm adding it to the Placeholder because I don't...
1
by: Earl Teigrob | last post by:
PROBLEM: When a user control is loaded into a PlaceHolder control more than once, the events do not fire on the first click of a control on the dynamically loaded user control. In other words, the...
7
by: bredal Jensen | last post by:
Hello, I have populated a web form with dynamically created radiobuttons in a table. I wnat some of the radio buttons to fire events when their ..CheckChanged property has changed. I understand...
5
by: karthick raja | last post by:
Am experiencing a problem intercepting the events from controls added dynamically to a Placeholder control at runtime. Is there any way that I can write an event handler on the page which will be...
1
by: Bishop | last post by:
I have a page that generates a grid of controls; at the end of each row is an update button that is dynamically created with an associated click event. The grid changes based on the date selected...
9
by: Erik Frey | last post by:
Hi there, Just curious as to whether there's a clever way to see the events a control/object is firing off, perhaps written out to the debug console. It would be really handy to know which...
3
by: Mark | last post by:
Assume you want to dynamically add one to many link button controls to a web page dynamically at run time. Each link button needs to post back and execute code. As the link buttons are created at...
5
by: Amoril | last post by:
I've read quite a few different message on various boards and for some reason I'm still having trouble wrapping my head around this viewstate maintenance and trying to get these dynamically created...
2
by: Nathan Sokalski | last post by:
I have LinkButtons that are dynamically created in one of the PostBack events. They must be created in the PostBack event because one of the variables required to determine which ones to create...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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
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,...

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.