Nov 22, 2009

Inserting multiple lines of text with innerHTML

I know there have been several posts about the DOM but I found a fabulous site that, in my opinion, is better than the W3Schools stuff. I have been trying to figure out how to display multiple Events using innerHTML with no success. Every time I looped through the Events all I would end up with is the last one. I thought about using the createElement, createTextNode and appendChild methods but that would involve re-thinking things and It was just not real clear how that all works.

I have looked for better explanations about some of these functions, especially innerHTML. Till I stumbled on this site it seemed like I wasn’t going to get it to work and would end up createElement, createTextNode and appendChild to displayed event information.

This is what I did to get innerHTML to work with multiple Events:

for (i =0; i < something.length; i++) // loop
{
// initialcontent stores the initial text in my "eventDetails" DIV
var initialcontent = document.getElementById("eventDetails").innerHTML;

// I put some text into the evnetDetails DIV
var state = xmlDoc.getElementsByTagName('state')[i].firstChild.nodeValue;
document.getElementById("eventDetails").innerHTML = "State: " + state;

// finalcontent stores the information I just put in eventDetails DIV
var finalcontent = document.getElementById("eventDetails").innerHTML;

// I use innerHTML to display initialcontent + finalcontent
document.getElementById("eventDetails").innerHTML = initialcontent + finalcontent;
}
// end for loop

I have run accross quite a few comments stating that we shouldn't use innerHTML.

I may try and use createElement, createTextNode and appendChild and what ever else I need to displayed event information.

This site has excellent explainations and examples on how use, from what I can tell, all the DOM functions.

5 comments:

  1. I was just trying to figure out how to do the same thing. I will have to take a look at that site. Thanks for the post!

    ReplyDelete
  2. I guess I'm not totally understanding what you're trying to do with "multiple lines via innerHTML," but from the sounds of it all you want to do is be able to submit multiple lines into your HTML via your JS. If that's the case, why not just do something like:

    document.getElemenetById(id).innerHTML="text1. \n text2."

    The \n will break it to a new line....is that all you're trying to do? I don't think it is, but maybe it will help...

    ReplyDelete
  3. Matt, I guess it isn't clear because I left a little code out but I will fix that later. It may become more clear after that. The problem I ran into was every time I got information from the DOM, say city, state and zip, by iterating through it I would only end up with the last city, state and zip being displayed.

    ReplyDelete
  4. Oooh, got ya! Well in that case, excellent solution! :)

    ReplyDelete
  5. Hey, I did take a look at the site and you are right, there are bunch of useful things that we could use in our projects. I will definitely check this site in dept. Thank for the head up.

    ReplyDelete