PDA

View Full Version : how to insert text or responseText to the div by using "appendChild" ?



smansakra
12-05-2008, 09:02 AM
how to insert text or responseText to the div by using "appendChild" ?

i have a div where i want to insert responseText to.

<div id="container">

<div id="div_1">
i love music..
i love mtv too
</div>

<div id="div_2">
i love you...
i love my self tooo...
</div>

</div>

MY problem here is,
i don't know how to insert "text/ responseText" to the place before <div id="div_1"> :mad:

if i use this code, it will insert the "text" after <div id="div_2">i love you...i love my self tooo...</div>


code:

var d = document.createElement('DIV');
d.innerHTML = myajax.responseText;
document.getElementById('container').appendChild(d);

can anyone help?:confused:

jscheuer1
12-05-2008, 10:03 AM
var d = document.createElement('DIV');
d.innerHTML = myajax.responseText;
document.getElementById('container').insertBefore(d, document.getElementById('div_1'));

Or, if you want to always insert to the beginning regardless of what's there:


var d = document.createElement('DIV');
d.innerHTML = myajax.responseText;
document.getElementById('container').insertBefore(d, document.getElementById('container').firstChild);

smansakra
12-05-2008, 10:56 AM
yeah!
thanks, i'm new in ajax and javascript, so i don't know about "insertBefore"
thanks :rolleyes:

boogyman
12-05-2008, 02:52 PM
insertBefore essentially allows the developer, you, to place an element anywhere on the page. It takes two parameters, the first is the element that you wish to place, and the second is the placeholder or the element you wish to insert before.

therefore,



document.getElementById('container').insertBefore(d, document.getElementById('container').firstChild);


states that it will be inserting the element defined by the variable d




var d = document.createElement('DIV');
d.innerHTML = myajax.responseText;


and it is being placed before the first element of the container element as defined by




document.getElementById('container').firstChild

smansakra
12-06-2008, 09:33 AM
thanks you very much :)