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

Share Improve this question edited Jul 4, 2013 at 13:57 Canttouchit asked Jul 4, 2013 at 13:53 CanttouchitCanttouchit 3,1596 gold badges39 silver badges52 bronze badges 4
  • 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
Add a ment  | 

3 Answers 3

Reset to default 3

First 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

Share Improve this question edited Jul 4, 2013 at 13:57 Canttouchit asked Jul 4, 2013 at 13:53 CanttouchitCanttouchit 3,1596 gold badges39 silver badges52 bronze badges 4
  • 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
Add a ment  | 

3 Answers 3

Reset to default 3

First 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

本文标签: