javascript - The space doesn't counted in paragraph.innerHTML? -


i've been creating website can store variable whenever user type in input box. can see code below used creating website.

function myfunction() {      var x = document.getelementbyid("mytext").value;      document.getelementbyid("demo").innerhtml = x;      document.getelementbyid("mytext2").value = x;  }
first name: <input type="text" id="mytext" value="   mickey">  </br>  </br>  copy name: <input type="text" id="mytext2">  <p>click button display value of value attribute of text field.</p>    <button onclick="myfunction()">try it</button>    <p id="demo"></p>

although code works want, found bug. here's bug, when type space several times in input box, why outcome different when stored demo paragraph has same result input box ? space counted in input box not in demo paragraph. , how make total space in input box same paragraph.innerhtml when stored ?

wrap html code in <span class="data"> tags , add following css,

span.data { white-space: pre; } 

duplicate:

html/css - best practice preserving white space on elements?


Comments

Popular posts from this blog

java - Suppress Jboss version details from HTTP error response -

gridview - Yii2 DataPorivider $totalSum for a column -

Sass watch command compiles .scss files before full sftp upload -