/    Sign up×
Community /Pin to ProfileBookmark

How to simulate the return key in innerHTML?

I am stuck with a dead simple problem: how to make line breaks when writing in a DIV with an innerHTML command captured in a text area receiving keystrokes events ?

take a look at this very simple example:

[url]http://www.pixclinic.com/inner.html[/url]

you will see that inserting a <br> tag doesn’t work as expected (I coded it to insert a <BR>$ to better display what is happening: I just don’t get it! my <br>? is replaced by a space after the next keystroke

Thanks everybody!

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@CrazyMerlinJul 13.2007 — A carriage return is simply [B]"n"[/B]

You need to change your code though as you are only writing it to work in IE, it will not work in FF like that.

[B]event.keyCode[/B] is IE-specific, other browsers use [B]event.which[/B], so you need to include something like a [B]if(document.all)[/B] to detect for IE
Copy linkTweet thisAlerts:
@pixclinicauthorJul 13.2007 — Hi Crazymerlin

Thanks for the cross-browser tip.Let's pretend for now that FF has not been invented yet, and let's make it work for IE :-)

I tried r and n before with no sucess. <br> even works better, as I can see the carriage return during the keystroke on return. but nothig happens in this case. Could you please look at my link and let me know what I'm doing wrong?

I will work on the FF compatibily when I'm sure I can do it in one browser first

Thanks
Copy linkTweet thisAlerts:
@steveaJul 13.2007 — Here is a cross-browser friendly way of accomplishing what you're looking for.

It's going to cause a bit of a performance hit because it's creating new DOM textNodes for every line but it was the firs thing out of my head.

<i>
</i>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd"&gt;
&lt;HTML&gt;
&lt;HEAD&gt;
&lt;TITLE&gt;Test Page&lt;/TITLE&gt;
&lt;style type="text/css"&gt;
#displayText { width: 300px; height: 100px; background: #eee; overflow: auto }

<i> </i>&lt;/style&gt;
<i> </i>&lt;script type="text/javascript"&gt;
<i> </i> function placeText(){
<i> </i> var div = document.getElementById('displayText');
<i> </i> var txtbox = document.getElementById('typeHere');
<i> </i> var typed = new String(txtbox.value);
<i> </i> typed = typed.split('n');
<i> </i> while(div.childNodes.length &gt; 0){ div.removeChild(div.childNodes[0]); }
<i> </i> for( var i = 0; i&lt;typed.length; i++ ){
<i> </i> div.appendChild(document.createTextNode(typed[i]));
<i> </i> div.appendChild(document.createElement('BR'));
<i> </i> }
<i> </i> }
<i> </i>&lt;/script&gt;
&lt;/HEAD&gt;
&lt;BODY&gt;
&lt;textarea id="typeHere" cols="40" rows="6" onkeyup="placeText();" onfocus="this.value=this.value=='Begin typing here'?'':this.value;"&gt;Begin typing here&lt;/textarea&gt;
&lt;div id="displayText"&gt;&lt;/div&gt;
&lt;/BODY&gt;
&lt;/HTML&gt;


Cheers.
Copy linkTweet thisAlerts:
@pixclinicauthorJul 13.2007 — Hi Stevea

this one rocks :-)

I just found a quicker solution, neither using keystroke events nor nodes:

$text = $text.replace(/n/g,'<br>');

that was dead simple :-)
×

Success!

Help @pixclinic spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...