admin管理员组文章数量:1022935
I have the following code:
<body>
<form>
<textarea id="textfield"></textarea>
<input type="button" onclick="func1()" value="Post">
</form>
<p id="para"></p>
</body>
When I type in textarea all the special tags <a>,<br>
etc are ignored when I display them inside a <p>
also all what I typed is displayed on one line and it doesn't even matter that I pressed return or use <br>
, textarea seems to be taking html tags and turn them into a simple text.
This is the function I use to display the text area in html:
function func1()
{
document.getElementById("para").innerHTML=document.getElementById("textfield").innerHTML;
}
How do I take text from textarea and display it on screen normally(not in one line).
How do I modify textarea for a user who doesn't know how to use tags? when pressing a simple return should be translated to <br
I have the following code:
<body>
<form>
<textarea id="textfield"></textarea>
<input type="button" onclick="func1()" value="Post">
</form>
<p id="para"></p>
</body>
When I type in textarea all the special tags <a>,<br>
etc are ignored when I display them inside a <p>
also all what I typed is displayed on one line and it doesn't even matter that I pressed return or use <br>
, textarea seems to be taking html tags and turn them into a simple text.
This is the function I use to display the text area in html:
function func1()
{
document.getElementById("para").innerHTML=document.getElementById("textfield").innerHTML;
}
How do I take text from textarea and display it on screen normally(not in one line).
How do I modify textarea for a user who doesn't know how to use tags? when pressing a simple return should be translated to <br
- You need to use <html> tags that do what they actually do? – Mohammad Areeb Siddiqui Commented Jul 4, 2013 at 13:55
- 1 Take a look at these: bbcode/examples – Mohammad Areeb Siddiqui Commented Jul 4, 2013 at 13:57
- You don't understand my question, textarea is ignoring my html tags it displays the html tags as if it was a simple text – Canttouchit Commented Jul 4, 2013 at 13:59
-
1
HTML tags are not rendered in a text area. Use
\n
. – George Cummins Commented Jul 4, 2013 at 13:59
3 Answers
Reset to default 3First of all, for textareas you should use value
and not innerHTML
. Like this...
document.getElementById("para").innerHTML=document.getElementById("textfield").value;
Now, for the single line issue. In textarea, new lines are separated by \n. In your divs \n do not work. So you'll have to replace them with <br>
tags. So rewriting your code snippet...
document.getElementById("para").innerHTML=document.getElementById("textfield").value.replace(/\r\n|\r|\n/g,"<br />");
The text in the textarea isn't html. It is just text, containing regular line break "\n"
. To display them, you either need to enclose the text in a pre
tag, or replace the "\n"
with <br>
.
I would do the latter, since pre
doesn't break at all if there's no break in the text, so you'll have a single long line and a scrollbar.
<textarea>
does not take HTML and interprets it. I think what you are looking for is a Rich Text Editors if you want your users to be able to modify text without knowing the tag names
I have the following code:
<body>
<form>
<textarea id="textfield"></textarea>
<input type="button" onclick="func1()" value="Post">
</form>
<p id="para"></p>
</body>
When I type in textarea all the special tags <a>,<br>
etc are ignored when I display them inside a <p>
also all what I typed is displayed on one line and it doesn't even matter that I pressed return or use <br>
, textarea seems to be taking html tags and turn them into a simple text.
This is the function I use to display the text area in html:
function func1()
{
document.getElementById("para").innerHTML=document.getElementById("textfield").innerHTML;
}
How do I take text from textarea and display it on screen normally(not in one line).
How do I modify textarea for a user who doesn't know how to use tags? when pressing a simple return should be translated to <br
I have the following code:
<body>
<form>
<textarea id="textfield"></textarea>
<input type="button" onclick="func1()" value="Post">
</form>
<p id="para"></p>
</body>
When I type in textarea all the special tags <a>,<br>
etc are ignored when I display them inside a <p>
also all what I typed is displayed on one line and it doesn't even matter that I pressed return or use <br>
, textarea seems to be taking html tags and turn them into a simple text.
This is the function I use to display the text area in html:
function func1()
{
document.getElementById("para").innerHTML=document.getElementById("textfield").innerHTML;
}
How do I take text from textarea and display it on screen normally(not in one line).
How do I modify textarea for a user who doesn't know how to use tags? when pressing a simple return should be translated to <br
- You need to use <html> tags that do what they actually do? – Mohammad Areeb Siddiqui Commented Jul 4, 2013 at 13:55
- 1 Take a look at these: bbcode/examples – Mohammad Areeb Siddiqui Commented Jul 4, 2013 at 13:57
- You don't understand my question, textarea is ignoring my html tags it displays the html tags as if it was a simple text – Canttouchit Commented Jul 4, 2013 at 13:59
-
1
HTML tags are not rendered in a text area. Use
\n
. – George Cummins Commented Jul 4, 2013 at 13:59
3 Answers
Reset to default 3First of all, for textareas you should use value
and not innerHTML
. Like this...
document.getElementById("para").innerHTML=document.getElementById("textfield").value;
Now, for the single line issue. In textarea, new lines are separated by \n. In your divs \n do not work. So you'll have to replace them with <br>
tags. So rewriting your code snippet...
document.getElementById("para").innerHTML=document.getElementById("textfield").value.replace(/\r\n|\r|\n/g,"<br />");
The text in the textarea isn't html. It is just text, containing regular line break "\n"
. To display them, you either need to enclose the text in a pre
tag, or replace the "\n"
with <br>
.
I would do the latter, since pre
doesn't break at all if there's no break in the text, so you'll have a single long line and a scrollbar.
<textarea>
does not take HTML and interprets it. I think what you are looking for is a Rich Text Editors if you want your users to be able to modify text without knowing the tag names
本文标签:
版权声明:本文标题:javascript - Why when I try to display the text inside a textarea on html it display eveything in one line? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745563485a2156306.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论