简体   繁体   中英

Send html as value to textarea

I have a textbox where I put info to get new info based on that search using the folling html & script:

HTML:

<input name="line" placeholder="Line" onkeydown="if (event.keyCode == 13) { calculate(); return false; }" style="width:40px;" type="textbox">
<input value="Reset" type="reset">
<input id="call" placeholder="Entrepreneur" style="width:95px;" type="textbox">
<input id="number" placeholder="Phonenumber" style="width:110px;" type="textbox">

SCRIPT:

function calculate() {
  var line = document.buss.line.value;
  var ent;
  var num;

  switch(line) {
    case "12":
    case "27":
    case "30":
    <-- a lot of cases -->
      ent = "Entrepreneur";
      num = "Phonenumber";
      break;
    case "42":
    case "63":
    case "69":
    <-- a lot of cases -->
    ent = "Entrepreneur";
    num = "Number";
    break;      
    default:
  }
    document.buss.call.value = ent;
    document.buss.number.value = num;
}

All this works perfect but now I needed to put in some new information in a textarea but I can not get it to print the way I want. What I want is for the information to incude and
but right now it prints this as text instead of html, is there a way to change this? also is there a way to print it directly to the page instead of having a lot of textboxes and textarea?

HTML:

<input name="line" placeholder="Line" onkeydown="if (event.keyCode == 13) { calculate(); return false; }" style="width:40px;" type="textbox">
<input value="Reset" type="reset">
<input id="call" placeholder="Entrepreneur" style="width:114px;" type="textbox">
<input id="number" placeholder="Number" style="width:90px;" type="textbox">
<textarea id="stop" placeholder="Stop" style="width:300px; height:650px"></textarea>

SCRIPT:

function calculate() {
  var line = document.buss.line.value;
  var ent;
  var num;
  var stop;

  switch(line) {
    case "1":
      ent = "Entrepreneur";
      num = "Number";
      stop = "<b>stop1</b><br>stop2<br>stop3<br><b>stop4</b>";
      break;
    case "2":
      ent = "Entrepreneur";
      num = "Number";
      stop = "<b>stop1</b><br>stop2<br>stop3<br><b>stop4</b>";
      break;
    default:
  }
    document.buss.call.value = ent;
    document.buss.number.value = num;
    document.buss.stop.value = stop;
}

You can't render html inside a Textarea object. If you dont need the text to be editable, you can replace the inputs and textarea with div ou span tags, and replace it's content.

Example:

If you replace this:

<textarea id="stop" placeholder="Stop" style="width:300px; height:650px"></textarea>

With this:

<div id="stop" style="width:300px; height:650px">Stop</div>

And replace this:

document.buss.stop.value = stop;

With this:

document.getElementById('stop').innerHTML = stop;

The html is rendered.

To make the Reset Button work, you will need to add a reset function. This should do the trick:

<input onclick="resetHtml()" value="Reset" type="reset">

function resetHtml() {
    document.getElementById('stop').innerHTML = "Stop";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM