0

How do I take a string and create an html fragment with it in IE. This works fine in non-ie browsers.

var str = "<div>Foo</div>";
var range = document.createRange();
var frag = range.createContextualFragment(str);
var e = document.getElementById("element");
e.appendChild(frag);
2
  • createContextualFragment is not at all necessary considering the widespread adoption of innerHTML. Here are a couple answers that use document fragments to avoid the dangling extra div you'd get with a straight innerHTML approach: stackoverflow.com/questions/814564/… and stackoverflow.com/questions/788614/… Commented Jan 22, 2010 at 19:56
  • 1
    innerHTML has quite a few idiosyncracies across the major browsers. I'd really like to know how to do this using w3c compliant code. Commented Feb 4, 2010 at 22:48

1 Answer 1

1

You may want to consider using jQuery? Cross-browser compatible and far easier to use:

$(document).ready(function() {
  var str = "<div>Foo</div>";
  $('#element').append(str); //assuming your element's id is 'element'
});

jQuery site and documentation

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

Comments

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.