2

In html, how to count the real characters shown in a innerHTML?

For example, In <strong>Hi</strong>, the # of characters should be 2, not 19.

I've tried 2 methods which do not work:

  1. str.length - This will include <> characters.
  2. getElementById - This will include <> characters:

I tried as follows:

elem_output = document.getElementById("example_id"); 
elem_output.innerText.length;//1st
elem_output.textContent.length;//2nd
Bakhtiiar Muzakparov
  • 2,308
  • 2
  • 15
  • 24
Charles Zha
  • 169
  • 2
  • 4
  • 17

6 Answers6

5

I don't know which element has #example_id in your code, but element.innerText.length should work.

Edit: Other answers suggest the use of textContent instead of innerText. I think that depending on OP's real needs one or the other (or even innerHTML, which appears in question's title) could be suitable. So, I'd suggest to read carefully the differences between those properties in the docs:

Edit 2: OP's question says real characters shown in a innerHTML, so I'd go with innerText since:

innerText is aware of style and will not return the text of hidden elements, whereas textContent will.

var tag = document.getElementById('test');
console.log(tag.innerText.length)
<strong id="test">Hi</strong>
Jordi Nebot
  • 3,355
  • 3
  • 27
  • 56
  • 1
    Great, thanks. This works! I should not use `elem_output.innerText.length | elem_output.textContent.length;` only the `elem_output.innerText.length` part is enough. – Charles Zha Jul 24 '17 at 17:07
  • Sure. `|` is the [bitwise OR](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#Bitwise_OR) operator, and it doesn't make any sense to me on your code... – Jordi Nebot Jul 24 '17 at 17:11
1

var html='<div>div1</div><div>div2</div>';
console.log( 
    html.replace(/<(?:.|\n)*?>/gm, '').length
);
Jarek Kulikowski
  • 1,399
  • 8
  • 9
1

Try this

var p=document.createElement("p")
p.innerHTML="<strong>Hi</strong>"
alert(p.querySelector("strong").innerHTML.length)

If you have data in html then you can also try following

alert(document.getElementsByTagName("strong")[0].innerText.length);
//Or
alert(document.getElementsByTagName("strong")[0].innerHTML.length);
//Or
alert(document.querySelector("strong").innerHTML.length);
//Or
alert(document.querySelector("strong").innerText.length);
<strong>Hi</strong>
Sourabh Somani
  • 2,138
  • 1
  • 13
  • 27
1

I guess there can be a lot of different html tags within your text so there's three way of doing things

If you need to use regex to filter specific tags then innerHTML would be the way to get what you need.

var exampleid = document.getElementById('example_id').innerHTML;
exampleid = exampleid.replace(/<[^>]strong>/g, ""); 
alert(exampleid.length + ' characters.');
<div id="example_id">hi, <strong>I'm bold and my tags are ignored</strong> while <i>I'm Italic but my tags are counted</i>.</div>

See : Regular Expression: exclude html tags from "content"


If you wish to ingore all tags then use innerText or textContent.

innerText :

var exampleid = document.getElementById('example_id').innerText.length;
alert(exampleid + ' characters.');
<div id="example_id">hi, <strong>I'm bold</strong> while <i>I'm Italic</i>.</div>

textContent (careful textContent a W3C standard, it is not supported by IE < 9) :

var exampleid = document.getElementById('example_id').textContent.length;
alert(exampleid + ' characters.');
<div id="example_id">hi, <strong>I'm bold</strong> while <i>I'm Italic</i>.</div>

Learn more on the differences between all these : Difference between innerText and innerHTML in javascript

Horai Nuri
  • 5,358
  • 16
  • 75
  • 127
  • There's absolutely no need for this. Both `innerText` and `textContent` get **rendered text** for the node and its children. Check the console on this [JSFiddle](https://jsfiddle.net/jordinebot/u1okch3k/) – Jordi Nebot Jul 24 '17 at 17:45
  • 1
    @JordiNebot Thanks for your comment, I updated my answer. – Horai Nuri Jul 24 '17 at 17:57
1

What about this for <strong>Hi</strong>

document.getElementsByTagName("strong").innerText.length;
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
0

You want to use Node.textContent.

var element = document.querySelector('#some-element');
var length = element.textContent.length;

It's the simplest and elegant solution.

kamyl
  • 5,938
  • 4
  • 23
  • 29