Create Line Break in Form TextArea

SaltyDawg
SaltyDawg used Ask the Experts™
on
I have a form with a textarea. When a user clicks on a checkbox it add text to the top of the textarea field using a javascript. I can do all this, but what I would like to be able to do is create a line break after inserting the text.
function ExpiditedShipping()
	{
		var chkExpiditedShipping = document.getElementsByName("chkExpiditedShipping");
		var txtOrderComment = document.getElementsByName("txtOrderComment");
		var myNewString = frmSumbitOrder.txtOrderComment.value;
		//
		if(chkExpiditedShipping[0].checked == true)
		{
			frmSumbitOrder.txtOrderComment.value = "REQUEST FOR EXPIDITED SHIPPING: " + myNewString;
			return true;
		}
		else
		{
			myNewString = myNewString.replace("REQUEST FOR EXPIDITED SHIPPING: ", "")
			frmSumbitOrder.txtOrderComment.value = myNewString;
			return true
		}
		return false;
	}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
HonorGodSoftware Engineer
Commented:
Like this?
<html>
<body>
 
<textarea id='here' rows="10" cols="20"></textarea><br>
<input type=button value='Add Text' onclick='AddText()'>
 
<script type="text/javascript">
  function AddText() {
    var here = document.getElementById( 'here' );
    for ( var i = 1; i < 6; i++ ) {
      here.value += 'Line ' + i + '\n';
    }
  }
</script>
 
</body>
</html>

Open in new window

Give a id to the text area code

like;

<input ..... #idCode />

In CSS file,

#idCode {
   clear:both;
}
Commented:
Assuming the javascript you listed above works for you, all you need to do is add \n to the end of your strings, like the following.

That being said, I couldn't get it working with getElementsByName, so I used getElementsById for my personal testing. I've left it as getElementsByName in the script, however, as you indicated it was working fine for you except for the line break.
function ExpiditedShipping()
	{
		var chkExpiditedShipping = document.getElementsByName("chkExpiditedShipping");
		var txtOrderComment = document.getElementsByName("txtOrderComment");
		var myNewString = frmSumbitOrder.txtOrderComment.value;
		//
		if(chkExpiditedShipping[0].checked == true)
		{
			frmSumbitOrder.txtOrderComment.value = "REQUEST FOR EXPIDITED SHIPPING: " + myNewString + "\n";
			return true;
		}
		else
		{
			myNewString = myNewString.replace("REQUEST FOR EXPIDITED SHIPPING: ", "")
			frmSumbitOrder.txtOrderComment.value = myNewString + "\n";
			return true
		}
		return false;
	}

Open in new window

Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

HonorGodSoftware Engineer

Commented:
Thanks for the assist and the grade.

Good luck & have a great day.

Author

Commented:
No problem I found my own solution based on each of you guy's posts.
HonorGodSoftware Engineer

Commented:
oh?  Please share what you learned...

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial