Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Removing Whitespace When It Shouldn't?

I have a HTML file that has code similar to the following.

<table>
    <tr>
    <td id="MyCell">Hello  World</td>
    </tr>
</table>

I am using javascript like the following to get the value

document.getElementById(cell2.Element.id).innerText

This returns the text "Hello World" with only 1 space between hello and world. I MUST keep the same number of spaces, is there any way for that to be done?

I've tried using innerHTML, outerHTML and similar items, but I'm having no luck.

like image 805
Mitchel Sellers Avatar asked Oct 17 '08 21:10

Mitchel Sellers


People also ask

Does JavaScript care about white space?

Normally in JavaScript space does not matter. However, in your case, since you are gluing “cake” to length by a dot, it should be just one word all together to avoid ambiguity or a bad interpretation from JavaScript. After length, the space between “cake”. length and * or between * and 9 does not matter.

How do you clear a space in JavaScript?

JavaScript String trim() The trim() method removes whitespace from both sides of a string.

What is the best way to remove whitespace characters from the start or end?

Use the . strip() method to remove whitespace and characters from the beginning and the end of a string. Use the . lstrip() method to remove whitespace and characters only from the beginning of a string.


2 Answers

HTML is white space insensititive which means your DOM is too. Would wrapping your "Hello World" in pre block work at all?

like image 128
Nick Avatar answered Sep 28 '22 04:09

Nick


In HTML,any spaces >1 are ignored, both in displaying text and in retrieving it via the DOM. The only guaranteed way to maintain spaces it to use a non-breaking space &nbsp;.

like image 33
TJ L Avatar answered Sep 28 '22 04:09

TJ L