4

I have a figcaption and i would like the text from it. This is what it looks like in the element inspector in chrome. For some reason the 'innerHTML' starts with a return and then a lot of spaces. The 'innerText' however looks good so i'm interested in that.

enter image description here

Online I see I have to use the text() method to get the innerText but it seems to give the innerHTML:

console.log("-"+$(this).find("figcaption").text()+"-");

enter image description here

So how can I get to the innerText?

6
  • 3
    I think it is indeed returning the innerText. Notice that there are no html tags in the resulting output. Commented Nov 3, 2014 at 18:26
  • You could use replace after text(), like: .replace(/( ){2,}/gi, "red"). It will remove all extra spaces. Commented Nov 3, 2014 at 18:32
  • @Kolban But there are no spaces in the innerText in the dom. Commented Nov 3, 2014 at 18:41
  • @klauskpm nice, this brings me somewhere. I only have a return at the start. Could I also get rid of that. And this is just modifying the innerHTML instead of the innerText right? Commented Nov 3, 2014 at 18:42
  • Well, text() is supposed to return innerText. You are also receving return as part of the text? Commented Nov 3, 2014 at 19:04

1 Answer 1

7

When you create an HTML element such as:

<p>Hello          World</p>

When that is rendered by the browser, whitespace is collapsed by default. The result will be "Hello World". That collapsing of the data happens at browser rendering time. The DOM model contained within the browser maintains the actual text including any white spaces contained within it. As such, what you see on the screen may not be the same as the data contained within the DOM.

The white space stripped data is available via the DOM property called "innerText".

A jsBin sample showing the data has been supplied.

The demonstration code is basically:

HTML

<p id="here">Hello      World</p>
<button>Show</button>

JavaScript

$(function() {
  $("button").click(function() {
    console.log("text: " + $("#here").text());
    console.log("innerText: " + $("#here").prop("innerText"));
  });
});

The console will log:

text: Hello      World
innerText: Hello World

From your original question, the use of the text() method returns the DOM data while the use of prop("innerText") returns the calculated innerText value that the browser built.

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

2 Comments

If I look in the dom (see screenshot OP), then innerHTML has those whitespace, innerText however has not. So is assume innerText is ok without having the need to edit. In case it matters, it's the text from adding photo's to tumblr. So those added whitespaces make no sense anyway.
Thanks for the comment. Ive updated the answer with more details and a sample.

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.