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 badges2 Answers
Reset to default 1There'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条)