Reputation: 379
I'm using JavaMail to send emails. So I've got a textArea where customers can compose their messages and send them to me on button press. During my testing I realize that when I press "Enter"/"Return" key inside my textArea it breaks my text but when I check mail box it just one line text!
How to do it?
Please help me guys!
I' using this code for now and it seems working:
<textarea rows="10" id="Message" onkeypress="BreakLine();"></textarea>
<script type="text/javascript">
function BreakLine() {
var key = window.event.keyCode;
// If the user has pressed enter
if (key == 13) {
document.getElementById("Message").value = document.getElementById("Message").value + "<br/>";
}
}
</script>
But when I taped something inside textArea it prints "<br/>"
along with the rest text as well but when I receive email its fine I can see breaks in the text like it suppose to be! But how to get rid of "<br/>"
inside textArea?
I found this example for outlook (which one I'm using) as well and it uses "\r\n"
instead of "<br/>"
but it doesn't work for me!
P.S. I know above example will work only for IE but don't worry about it for now...
Upvotes: 2
Views: 2944
Reputation: 36609
Try this:
<textarea rows="10" id="Message" onkeypress="BreakLine();"></textarea>
<script type="text/javascript">
function BreakLine() {
var key = window.event.keyCode;
// If the user has pressed enter
if (key == 13) {
document.getElementById("Message").value = document.getElementById("Message").value +"\n";
window.event.preventDefault();
}
console.log(encodeURIComponent(document.getElementById("Message").value));
}
</script>
Upvotes: 2
Reputation: 379
I solve it on my own, so on "Send Email" button click it checks first does text contain /n (break) if "yes" it adds line into array cell (lines). So every line are kept separately. And after that I add every line to hidden textArea value and add <br/>
in the end of each line.
Text Areas:
<textarea rows="10" id="Message" name="Message"></textarea>
<textarea id="HiddenMessage" name="HiddenMessage" style="display:none;"></textarea>
Button:
<input type="button" id="SendEmail" value="Send Email" onclick="BreakLine();">
JavaScript:
<script type="text/javascript">
function BreakLine() {
var lines = $('#Message').val().split('\n');
for(var i = 0;i < lines.length;i++){
document.getElementById("HiddenMessage").value = document.getElementById("HiddenMessage").value + lines[i] + "<br/>";
}
}
</script>
Upvotes: 0
Reputation: 5
If the message is text/plain
, then using \r\n
should work. Instead, if the message type is text\html
, use then use <p/>
.
Upvotes: 0
Reputation: 8158
Did you debug your code and see what string was being passed from the textArea to your code? By the way you can make your javascript cross-browser compatible:
function BreakLine() {
var key = event.which || event.charCode || event.keyCode;
// If the user has pressed enter
if (key == 13) {
document.getElementById("Message").value = document.getElementById("Message").value + "<br/>";
}
}
If the above line that gets the key does not work, you could always use jquery's "which" function to get the key code no matter what browser it is.
Edit:
You can combine the <br>
and the text in your text area and save this value in a hidden field on your page, instead of appending and showing it in your textarea. Then when the message is to be sent, you can get the message text with appended <br>
s from this hidden field and send it. This way there will be no annoying <br>
s in the text area plus your messages will be multi-line as you want.
Upvotes: 1