tags:

views:

210

answers:

5

hi , using obj.innerHTML = "abcxyz <[email protected]>" getting ouput: abcxyz the rest part is getting ignored because of angle bracket(<>). so how to achieve the same.

thanks in advace

+2  A: 

HTML encode your string or use HTML entities such as &lt;

robertc
... and there is also a builtin javascript function for that.
Hippo
... that is named how? escape() does URL encoding, if you had that in mind.
Boldewyn
I agree, don't think there's a built in function. There is a method in prototype.js: http://www.prototypejs.org/api/string/escapeHTML
robertc
+8  A: 

Actually, the content is still there, but the browser interprets it as unknown tag, that is, it does not display anything. Look at the generated page source (in FF, e.g., mark all text and use "Selection source" from the context menu).

Try quoting the brackets:

obj.innerHTML = "abcxyz <[email protected]>".replace (/</g, "&lt;")

This, however, will replace all <. If you want to embed other HTML, too, you will have to keep track on what you already encoded and what not.

Cheers,

Boldewyn
since i have to replace both the brackets(<>) so we cannt use some regular expression to replace the both
Abhimanyu
Try: obj.innerHTML = "abcxyz <[email protected]>".replace (/</g, "<").replace (/>/g, ">")
robertc
Actually you don't really *need* to replace the `>` as well, the HTML parser is savvy enough to keep it allone, if it is not preceeded by an `<`.
Boldewyn
s/allone/alone/
Boldewyn
+1  A: 

In HTML, a literal "<" is represented as "&lt;" and a literal ">" is represented as "&gt;". See HTML 4.01 section 5.3.2.

NickFitz
A: 

You need to escape < and > in string.

TheVillageIdiot
+1  A: 

I would try this: unescape('abcxyz %3Cabc...

G Berdal
How could that be of use? in you unescape it, you again have the `<` in your innerHTML string.
Boldewyn
You are right. Sorry. I misunderstood the problem.
G Berdal