Click to See Complete Forum and Search --> : writing to divs
Orsen Cart
07-08-2004, 02:28 PM
Hi
I have a div set up on a page (using CSS + overflow auto) and I would like to be able to display different text in that div area according to actions taken by the page user. I know how to write text into a textarea block using DOM but I can't seem to be able to do this with a div, despite giving it an id. Any suggestions?
Many thanks
M
David Harrison
07-08-2004, 02:35 PM
Here's the code:document.getElementById("id_of_div").appendChild(document.createTextNode("Blah blah blah!"));Here's an example that uses it:
Orsen Cart
07-08-2004, 03:53 PM
Hi Lava
Whilst i was trying that I came across ...
document.getElementById("menudesc").innerText = infoText[a];
which does exactly what I want.
Thanks for pointing me in the right direction.
M
Originally posted by Orsen Cart
document.getElementById("menudesc").innerText = infoText[a];
which does exactly what I want.In IE, perhaps. Did you try it in any other browser? What was wrong with what lavalamp posted?
Orsen Cart
07-08-2004, 05:37 PM
Hi Pyro
What lavalamp posted was fine apart from the fact that it appended text rather than replaced the text. I did try it with replaceChild but I couldn't get that to work.
I actually ended up using .innerHTML instead of .innerText as I wanted to be able to format the text in the DIV.
If there is a similar way to lavalamp's code to replace the text in the DIV, then that would be good.
Do you have some reason to believe that .innerText & .inner HTML may not work on other browsers and that lavalamp's approach will?
Many thanks
M
David Harrison
07-08-2004, 06:27 PM
Originally posted by Orsen Cart
What lavalamp posted was fine apart from the fact that it appended text rather than replaced the text.There was a replace function as well.
Orsen Cart
07-09-2004, 12:52 PM
Hi Lavalamp
Yes, I did find the replaceChild method too but I couldn't get that one to work. I tried....
function load(a).........
document.getElementById("menudesc").replaceChild(document.createTextNode(infoText[a]),document.getElementById("menudesc"));
Also, with the appendChild it was treating the text as plain text and hence displaying the HTML tags that I was using to format the text, so I figured replaceChild would be the same, so didn't spend that much time trying to make it work (once I had found the .innerHTML method.)
document.getElementById("menudesc").innerHTML = infoText[a]; may well be IE specific but it appears that 97% of traffic to my sites come via IE anyway. Is that % figure representative?
Thanks again
M
David Harrison
07-09-2004, 08:07 PM
Currently a mere 94% (http://www.thecounter.com/stats/2004/June/browser.php) of the web use IE (and it's falling).
When using replace child you have to specify which child to replace (which is the second arguement), what you did was reference the parent element again instead of a child node.
If you want to add elements into the div tag then you will have to use createElement like I did when I created a <br>.
Since you are just learning I would advise you to stick with innerHTML, it's easy to get a handle on and the hard part is done for you, but it's not valid and it's not possible to do everything using it.
Show me your code and I'll try and advise more.