user6866603 user6866603 - 2 months ago 6
HTML Question

How to insert twice of space "& nbsp " for each new line

I create an array with "split" and "join" when the array jump to a new line insert twice as much space as the previous and send it to a < p> by GetElementById.
This is my code:

<script type="text/javascript">
function myfunction() {

<p id="msg"></p>

var x= document.getElementById("input").value;

var text = x.split("\u0020").join(" <br/> ");

document.getElementById("msg").innerHTML = text;

}

</script>

Answer

consider using .reduce() instead of .join() - it gives much more flexibility at processing individual values and their concatenation. https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce

But before that, you are declaring your <p> element inside <script> tag - this most probably won't work at all, move it to somewhere before script tag.