2

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.

4
  • 5
    Search for "innerHMTL". There will be many examples and arguments for/against it. Commented Jul 10, 2012 at 21:17
  • I can't use innerHTML, as it does not apply any updates or functions or basically anything that's supposed to occur on load... Commented Jul 10, 2012 at 21:18
  • 3
    Explain your question better, everyone answered about innerHTML and I still think it solves your problem. Commented Jul 10, 2012 at 21:21
  • 1
    @Serge I wrote an answer with document.createTextNode() which may be what you're looking for Commented Jul 10, 2012 at 21:22

6 Answers 6

9

HTMLElement innerHTML Property

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

HTMLElementObject.innerHTML=text

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

Sign up to request clarification or add additional context in comments.

Comments

4

You can do it in two ways:

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

or object.innerHTML=text

Comments

3

I'm looking for a method to insert a string, which contains HTML data, into a div element.

What you want to use is the innerHTML property.

Example of use:

<script type="text/javascript">
function changeText(){
    document.getElementById('boldStuff').innerHTML = '<p>Universe</p>';
}
</script>
<p>Hello <b id='boldStuff'>World</b> </p> 
<input type='button' onclick='changeText()' value='Change Text'/>

Comments

1

do you mean like this? : http://jsfiddle.net/FgwWk/1 or do you have things in the div already before adding more?

Comments

1

Plain JS. Just use: element.insertAdjacentHTML(position, text);

position = "beforebegin" | "afterbegin" | "beforeend" | "afterend"

var text = '<a  class="btn btn-blue btn-floating waves-effect">\n' +
            '<i class="fas fa-user"><span class="badge badge-danger"></span></i>\n' +
            '</a>';
var inputPlace = document.getElementById("input-pace");

inputPlace.insertAdjacentHTML("beforeend", text);

Comments

0

One important advantage, that was not mentioned, that appendChild and insertAdjacentHTML have over insertHTML, is that they don't require the div element to be reparsed.

This is useful if the div already has other elements inside because, for example, if any of those elements have an event attached, they will lose it. Also, for this reason they are faster.

2 Comments

This is a great addition, but not an answer to the question. Maybe better as a comment, imo
@y-gherbi I know, I thought about that, but I can't comment because I'm new and I don't have enough reputation. So I thought that this was better than nothing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.