Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert HTML as a String, without JQuery

I'm looking for a method to insert a string, which contains HTML data, into a div element. The string is loaded via XHR, and there is no way of knowing what elements and such are in it.

I've searched around for a bit, and found some things that might help, but wouldn't completely work for me. What I need is something similar to the update() function from the Prototype framework: http://prototypejs.org/api/element/update

The platform I'm writing for does not allow frameworks to be used, or JQuery. I'm stuck with Javascript. Anyone have any ideas?

I can't use innerHTML, as it does not apply any updates or functions or basically anything that's supposed to occur on load

I have some onload events that need to occur, and as best I know, using innerHTML does not execute onload events. Am I incorrect?

EDIT 2 years later: For anyone else reading, I had some serious misconceptions about the onload event. I expected that it was a valid event for any element, while it is only valid for the <body/> element. .innerHTML is the proper method to do what I was looking for, and any extra functionality for the elements added, needs to be done manually some other way.

like image 960
Serge Avatar asked Nov 30 '25 05:11

Serge


2 Answers

HTMLElement innerHTML Property

The innerHTML property sets or returns the inner HTML of an element.

HTMLElementObject.innerHTML=text

Example: http://jsfiddle.net/xs4Yq/

like image 135
CD.. Avatar answered Dec 02 '25 18:12

CD..


You can do it in two ways:

var insertText = document.createTextNode(theText);
                 document.getElementById("#myid").appendChild(insertText);

or object.innerHTML=text

like image 42
cybertextron Avatar answered Dec 02 '25 18:12

cybertextron