Mailing List Archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [tlug] Javascript and I have different ideas about what "concatenate" means. [SOLVED]



> In any case, going with your code as a model, basically meticulously
> building the tree of nested divs with Javascript constructors, and then
> using appendChild to connect them all together, does result in the HTML
> getting concatenated together in the expected order.
> 
> It's a little tedious, but it works, and that's what's important.

Does it work in all browsers?

The rule of thumb for cross-browser development: always use jquery.
The rule of thumb for painless web development: always use jquery.

With JQuery your original code works exactly how you expected, and will
work in all browsers:

<div id="test"></div>
<script>
$('<div id="1"><div id="2"><div
id="3"></div></div></div>').appendTo('#test');
</script>


Here is a slightly expanded example to show it is indeed creating nested
divs:

<div id="test"></div>

<script>
$('<div id="1">111<div id="2">222<div
id="3">333</div>-222</div>-111</div>').appendTo('#test');
$('#3').click(function(){alert("Merry Christmas!")});
$('#2').click(function(){alert("Happy New Year!")});
</script>

(Tested in firefox 3.6 and Chromium 8; if it is not working in IE6/7/8
let me know as I have to dust off the windows machine later today anyway.)

Darren

P.S. appendTo() puts it after the last child of the given id. I.e. it
adds a child. prependTo() makes it first child.
Use insertBefore() and insertAfter() to make a sibling of the given id,
instead of a child.


-- 
Darren Cook, Software Researcher/Developer

http://dcook.org/gobet/  (Shodan Go Bet - who will win?)
http://dcook.org/work/ (About me and my work)
http://dcook.org/blogs.html (My blogs and articles)


Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links