Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

press "Enter" key and put <br> in a textbox

Status
Not open for further replies.

martinb7

Programmer
Jan 5, 2003
235
GB
Hi, i was wondering, how would you go about making a textarea insert <br> when you press the &quot;Enter&quot; key?

e.g.

Hi - b4 enter key

Hi<br> after enter key was pressed

any ideas?

Thanx

Martin
 
The below code should work for you.

<html>
<head>
<script language=&quot;javascript&quot;>
function InsertBreak(e){
//check for return key=13
if (parseInt(e.keyCode)==13) {
//get textarea object
var objTxtArea;
objTxtArea=document.getElementById(&quot;test&quot;);
//insert the existing text with the <br>
objTxtArea.innerText=objTxtArea.value+&quot;<br>&quot;;
}

}
</script>
</head>
<body >
<textarea id=&quot;test&quot; onkeydown=&quot;InsertBreak(event);&quot;></textarea>
</body>
</html>

MrGreed

&quot;did you just say Minkey?, yes that's what I said.&quot;
 
Thanx ;)

just a q, where did you get the key code for the ENTER key from?

Thanx

Martin
 
greed, will ur code stop the newline from being entered. it will caputre it but will it stop it?

Known is handfull, Unknown is worldfull
 
No it won't stop it, just capture it. Don't believe original post wanted to stop the enter key.

MrGreed

&quot;did you just say Minkey?, yes that's what I said.&quot;
 
Of course if the user's cursor is in the middle of the text, the &quot;<BR>&quot; will still get appended to the end. This script will insert a &quot;<BR>&quot; where the user's cursor is (IE only):

<HTML>
<HEAD>
<SCRIPT>
function storeCaret (textEl) {
if (textEl.createTextRange)
textEl.caretPos = document.selection.createRange().duplicate();
}
function insertAtCaret (textEl, text) {
if (textEl.createTextRange && textEl.caretPos) {
var caretPos = textEl.caretPos;
caretPos.text =
caretPos.text.charAt(caretPos.text.length - 1) == ' ' ?
text + ' ' : text;
}
else
textEl.value = text;
}
function checkEnter(textEl){
storeCaret(textEl);
if(window.event.keyCode==13){
insertAtCaret(textEl,&quot;<BR>&quot;);
}
}
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME=&quot;aForm&quot;>
<TEXTAREA NAME=&quot;aTextArea&quot; ROWS=&quot;5&quot; COLS=&quot;80&quot; WRAP=&quot;soft&quot;
ONSELECT=&quot;storeCaret(this);&quot;
ONCLICK=&quot;storeCaret(this);&quot;
ONKEYUP=&quot;checkEnter(this)&quot;></TEXTAREA>
</FORM>
</BODY>
</HTML>



Another option would be to search and replace all line breaks with &quot;<BR>&quot; when the textbox loses focus.

If you're going to display the text on your web page, I'd leave the original text alone and just search and replace the line breaks with &quot;<BR>&quot; at the time of display. In ASP it'd look something like <%=Replace(textVariable,vbCrLf,&quot;<BR>&quot;)%>

Yet another option would be to use <span contenteditable=&quot;true&quot;></span> and get the innerHTML of the span when they're done.

Adam
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top