Reputation: 389
I have a text area with the following id: #openingHours
The text area contains information, for example:
<textarea id="openingHours">
Mon-Fri 8am - 6pm
Sat-Sun 9am - 3pm
</textarea>
I want to get the value of the textarea and replace new lines with break tags.
Mon-Fri 8am - 6pm<br/>Sat-Sun 9am - 3pm
Notice how it is all on one line.
How can I achieve this? All of the data must be on the same line, separated by
tags and html encoded to special characters.
Thank you.
Upvotes: 0
Views: 2688
Reputation: 436
Try saving the following as an html and see how the line break in textarea works, tested on IE and Chrome
<script type="text/javascript">
function changeValue(foo)
{
var spvNotStr = "line1<br>line2<br>line3<br>";
var mySplitResult = spvNotStr.split("<br>");
for(i = 0; i < mySplitResult.length; i++){
document.forms[0].elements['foo'].value += mySplitResult[i]+'\n';
}
}
</script>
<form action="#">
<textarea name="foo" rows="10" cols="50"></textarea>
<hr />
<a href="#" onclick="changeValue('bar');">click</a>
</form>
Upvotes: 0
Reputation: 39872
You can use replace \n
with <br/>
<textarea id="openingHours">
Mon-Fri 8am - 6pm
Sat-Sun 9am - 3pm
</textarea>
var content = $('#openingHours').html().replace('\n', '<br/>');
Upvotes: 1
Reputation: 1748
You might be looking for
string htmlEncoded = Server.HtmlEncode(text);
In javascript do
function htmlEncode(value)
{
return $('<div/>').text(value).html();
}
sorry didnt see the javascript at first.
Upvotes: 1