Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdating InnerHTML of a DIV on Keypress event of a textbox
    text
    copied!<p>I am want to have a input box, where a user can add or change text, and at the same time (or after they have finished), I want to update the text inside a div, with the text they just typed.</p> <p>this is the code I used </p> <p><strong>JAVASCRIPT :</strong></p> <pre><code>&lt;script language="javascript" type="text/javascript"&gt; function change(boxid,divtoaffect) { content = document.getElementById("" + boxid + "").value; document.getElementById(divtoaffect).innerHTML = content; } &lt;/script&gt; </code></pre> <p><strong>HTML:</strong></p> <pre><code>&lt;table border="0" cellpadding="5" cellspacing="0" style="border-bottom:1px solid black; border-right:1px solid black; border-left:1px solid black; border-top:1px solid black" width="50%"&gt; &lt;tr&gt; &lt;td &gt;Name:&lt;br/&gt;&lt;input type="text" id="text1" onKeyPress="change('text1','output1')" /&gt;&lt;/td&gt; &lt;td valign="bottom"&gt;&lt;div id="output1" style="font-weight:bold;height:20px;"&gt;&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Designation:&lt;br/&gt;&lt;textarea id="text2" rows="1" onKeyPress="change('text2','output2')"&gt;&lt;/textarea&gt;&lt;/td&gt; &lt;td valign="bottom"&gt;&lt;div id="output2" style="height:40px;"&gt;&lt;/div&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Address:&lt;br/&gt;&lt;textarea id="text3" rows="2" onKeyPress="change('text3','output3')"&gt;&lt;/textarea&gt;&lt;/td&gt; &lt;td valign="bottom"&gt;&lt;div id="output3" style="height:50px;"&gt;&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; </code></pre> <p>The problem here is that it updates the DIV when I type the second word in the textbox,i.e when I type a single word it doesn't show up in the DIV and when I type the second one it shows the first word in the DIV.</p> <p><img src="https://i.stack.imgur.com/9pteQ.jpg" alt="enter image description here"></p> <p><img src="https://i.stack.imgur.com/xS1W8.jpg" alt="enter image description here"></p> <p>And also when I Press Enter in the Textarea field , in the DIV it continues on the same line. Here's the Image <img src="https://i.stack.imgur.com/g3Zde.png" alt="enter image description here"></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload