Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

innerHTML without the html, just text [duplicate]

I've created an e-mail link that automatically populates the necessary information in the body. But, when I do .innerHTML I get a little more than I bargained for.

I want "March, 2012: 12-16"

What I get <B>March, 2012</B>: <FONT color=blue>12</FONT> - <FONT color=blue>16</FONT>

Is there a way to get the innerHTML without the html tags?

.value = undefined .text = undefined 
like image 417
cphilpot Avatar asked Mar 15 '12 19:03

cphilpot


People also ask

Can you += innerHTML?

Appending to innerHTML is not supported: Usually, += is used for appending in JavaScript. But on appending to an Html tag using innerHTML, the whole tag is re-parsed.

How do I get text in innerHTML?

To set the value of innerHTML property, you use this syntax: element. innerHTML = newHTML; The setting will replace the existing content of an element with the new content.

Does innerHTML return a string?

Reading the HTML contents of an element Reading innerHTML causes the user agent to serialize the HTML or XML fragment comprised of the element's descendants. The resulting string is returned.

Why innerHTML should not be used?

The use of innerHTML creates a potential security risk for your website. Malicious users can use cross-site scripting (XSS) to add malicious client-side scripts that steal private user information stored in session cookies.


1 Answers

You want .textContent in all but older IE, and .innerText in IE (<9).

So, try:

string = (node.textContent===undefined) ? node.innerText : node.textContent;

EDIT: Or, just use GGG's much cleaner string = (node.innerText || node.textContent), since undefined is falsy.

like image 94
apsillers Avatar answered Oct 07 '22 15:10

apsillers