admin管理员组文章数量:1022603
I am entering some text in a input box. When entering a text I am giving some spaces means more then 1 space just say 4 or 5. But when I get the value from the input box and append to a div. It replaces all extra spaces with once single space.
Example, I enter text like this. "Hi can you please look on it ?"
then if get the value using $("#inpuboxid").val();
then append that value to div.
it bee like this: "Hi can you please look on it ?"
How to achieve this, as browser automatically replaces extra spaces with once space
I am entering some text in a input box. When entering a text I am giving some spaces means more then 1 space just say 4 or 5. But when I get the value from the input box and append to a div. It replaces all extra spaces with once single space.
Example, I enter text like this. "Hi can you please look on it ?"
then if get the value using $("#inpuboxid").val();
then append that value to div.
it bee like this: "Hi can you please look on it ?"
How to achieve this, as browser automatically replaces extra spaces with once space
Share Improve this question edited Aug 15, 2017 at 16:41 Cœur 38.8k25 gold badges206 silver badges279 bronze badges asked Feb 1, 2014 at 5:08 BoraBora 1,9253 gold badges28 silver badges55 bronze badges 6- 2 Ironically, stackoverflow does the same thing :p – Matt Way Commented Feb 1, 2014 at 5:11
-
1
insert
instead of space. and try again – Gopal Joshi Commented Feb 1, 2014 at 5:13 - 2 stackoverflow./questions/4503001/… – Matt Way Commented Feb 1, 2014 at 5:14
- @MWay What stackoverflow does the same ? – Bora Commented Feb 1, 2014 at 5:24
- @SureshBora Before your edit, stackoverflow also removed the spaces... – Matt Way Commented Feb 1, 2014 at 5:25
2 Answers
Reset to default 7HTML squishes adjacent spaces together by default. Add a style of "white-space: pre;" to your div. This should keep all the white space in its contents.
<div style="white-space: pre;">Hi can you please look on it ?</div>
See this Fiddle
After you set the text of the element, you can use the html method to replace spaces with
.
Here is an example.
$("#somelement").text($("#inpuboxid").val()).html(function(index,html){return html.replace(/ /g, ' ');})
I am entering some text in a input box. When entering a text I am giving some spaces means more then 1 space just say 4 or 5. But when I get the value from the input box and append to a div. It replaces all extra spaces with once single space.
Example, I enter text like this. "Hi can you please look on it ?"
then if get the value using $("#inpuboxid").val();
then append that value to div.
it bee like this: "Hi can you please look on it ?"
How to achieve this, as browser automatically replaces extra spaces with once space
I am entering some text in a input box. When entering a text I am giving some spaces means more then 1 space just say 4 or 5. But when I get the value from the input box and append to a div. It replaces all extra spaces with once single space.
Example, I enter text like this. "Hi can you please look on it ?"
then if get the value using $("#inpuboxid").val();
then append that value to div.
it bee like this: "Hi can you please look on it ?"
How to achieve this, as browser automatically replaces extra spaces with once space
Share Improve this question edited Aug 15, 2017 at 16:41 Cœur 38.8k25 gold badges206 silver badges279 bronze badges asked Feb 1, 2014 at 5:08 BoraBora 1,9253 gold badges28 silver badges55 bronze badges 6- 2 Ironically, stackoverflow does the same thing :p – Matt Way Commented Feb 1, 2014 at 5:11
-
1
insert
instead of space. and try again – Gopal Joshi Commented Feb 1, 2014 at 5:13 - 2 stackoverflow./questions/4503001/… – Matt Way Commented Feb 1, 2014 at 5:14
- @MWay What stackoverflow does the same ? – Bora Commented Feb 1, 2014 at 5:24
- @SureshBora Before your edit, stackoverflow also removed the spaces... – Matt Way Commented Feb 1, 2014 at 5:25
2 Answers
Reset to default 7HTML squishes adjacent spaces together by default. Add a style of "white-space: pre;" to your div. This should keep all the white space in its contents.
<div style="white-space: pre;">Hi can you please look on it ?</div>
See this Fiddle
After you set the text of the element, you can use the html method to replace spaces with
.
Here is an example.
$("#somelement").text($("#inpuboxid").val()).html(function(index,html){return html.replace(/ /g, ' ');})
本文标签: javascriptSpace not taking when displaying value in divStack Overflow
版权声明:本文标题:javascript - Space not taking when displaying value in div - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745569757a2156664.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论