...

View Full Version : Createelement - problems



bwuk
01-11-2007, 03:23 PM
Hi everyone,

I've recently started looking at AJAX and getting to grips with the DOM. Unfortunately, one thing I can't get my head around is the createelement.

Here's my dilemma:

I have a div, with some nested divs within it. For example:




<div id="container">
<div id="content_1">content 1</div>
<div id="content_2">content 2</div>
<div id="content_3">content 3</div>
<div id="content_4">content 4</div>
</div>


What I'm trying to do is make an AJAX request and with the response from the request, insert the response within the div id 'container'.

So, for example, if the response from the AJAX request was:



<div id="content_5">content 5</div>


I'd want that to appear within the 'container':



<div id="container">
<div id="content_1">content 1</div>
<div id="content_2">content 2</div>
<div id="content_3">content 3</div>
<div id="content_4">content 4</div>
<div id="content_5">content 5</div>
</div>


It can appear anywhere within the 'container'. It doesn't have to be at the bottom.

Can anyone help please?

Thanks everyone :)

Kor
01-11-2007, 03:42 PM
If you do not intend to change later the DOM tree, you may use non-standard innerHTML method


...
var AJAXresponse='<div id="content_5">content 5</div>';
var innerCont=document.getElementById('container').innerHTML;
innerCont+=AJAXresponse;
document.getElementById('container').innerHTML=innerCont;
...

otherwise you should use DOM methods (createElement(), setAttributes()... ), which is a little bit intricate, as you either have change the response or to split the string response in order to get the needed values to be used in DOM methods (the tag name, the attributes, the attributes' values...)

BubikolRamios
01-11-2007, 06:58 PM
the DOM:



var someDIV= getElementByID("container");
var groupArray = someDIV.getElementsByTagName("DIV");
someDIV.insertBefore(someDIV,groupArray[groupArray.length-1])


would place your div on exact position as in your example.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum