erikvold
erikvold

Reputation: 16528

How to adding special html chars without using innerHTML

So I'm working on a micro lib, html.js, and basically it creates text nodes with document.createTextNode but when I want to create a text node with a b I get a b so I'm wondering how to escape the & char, without using innerHTML ideally..

Upvotes: 2

Views: 312

Answers (2)

Mr Lister
Mr Lister

Reputation: 46579

Javascript supports the \uXXXX notation, so in the case of a non-breaking space, that would be \u00A0.

document.createTextNode('a\u00A0b');

That's as far as you can get. It's a text node, consisting only of text, and there's no difference between texts created from entity references or from normal characters.

If that's not what you want, you should take a second look at innerHtml. Can't you read it, modify it and put it back?

Upvotes: 4

Nick Rolando
Nick Rolando

Reputation: 26177

There's not much functionality in js to encode/decode html entities. Seems like there some libraries out there, though, that can help you achieve this. Here is one I found on goodle.. haven't tried it, but you can check it out, or look for others.

http://www.strictly-software.com/htmlencode

Upvotes: 0

Related Questions