Console Logging Inner Html As A Single-Line String In Javascript - Stack Overflow

I'm trying to grab the innerHTML of a DOM element and log it into a console as a single line, but

I'm trying to grab the innerHTML of a DOM element and log it into a console as a single line, but the console.log() function logs it with it's indentation as it appears on the page.

As simple example, on this article: .html?_r=0 I can retrieve the innerHTML of the first paragraph:

document.getElementById('story-continues-1').innerHTML

Running this in the chrome console returns the text as one long, single-line string, which is what I want. Except when I console.log() that same code:

console.log(document.getElementById('story-continues-1').innerHTML)

it gets returned in pretty, paragraph form, not as one long line. Help appreciated, thanks.

I'm trying to grab the innerHTML of a DOM element and log it into a console as a single line, but the console.log() function logs it with it's indentation as it appears on the page.

As simple example, on this article: http://www.nytimes./2015/11/27/world/europe/paris-attacks-have-many-in-france-eager-to-join-the-fight.html?_r=0 I can retrieve the innerHTML of the first paragraph:

document.getElementById('story-continues-1').innerHTML

Running this in the chrome console returns the text as one long, single-line string, which is what I want. Except when I console.log() that same code:

console.log(document.getElementById('story-continues-1').innerHTML)

it gets returned in pretty, paragraph form, not as one long line. Help appreciated, thanks.

Share asked Nov 26, 2015 at 16:17 DZackDZack 3091 gold badge2 silver badges7 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 1

There's no help here - it's how Chrome's console works. It simply won't allow horizontal scrolling and in your string "PARIS — The attacks by militants tied to the Islamic State less than two weeks ago in Paris have awakened a patriotic fervor in <a href="http://topics.nytimes./top/news/international/countriesandterritories/france/index.html?inline=nyt-geo" title="More news and information about France." class="meta-loc">France</a> not seen in decades." it simply breaks lines on spaces.

You can't change the way their console works, unless you build your own browser.

If you need just the text, you can do:

console.log(document.getElementById('story-continues-1').textContent)

Json.stringify (thanks Shomz) is what I was looking for. It reveals that the string contained hidden /n characters which were creating the new lines. Once those are removed, the strings logs in a single line as desired.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744924281a4601333.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信