Multi-line textarea jQuery.append

Written by James McDonald

April 24, 2013

I have a textarea which I use jquery.append() to place error messages into.

$('textarea').append(status_text + line_break);

I have found that in Internet Explorer if I use a newline character “\n” for the line_break I get:

success_one_line_newline

If I use a carriage return line feed “\r\n” in Internet Explorer I get a weird space inset.
success_stepped_return_newline

If I use <br /> I get what I’m wanting which is each append on it’s own line.
success_br

I discovered inconsistent results in Firefox and Google Chrome but for all three a stable configuration was using browser feature detection to detect the htmSerialize (which incidentally tells us if it’s IE or a FF/GC) and then return the correct line break.

// set <br /> for i.e and \r\n for FF, GC
// ff and gc return true
// ie returns false for this jquery.support test
function get_line_break() {

	return $.support.htmlSerialize ? "\r\n" : '<br />';

}

var line_break = get_line_break();

Success

 

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

You May Also Like…

How to Research a CPU Upgrade

How to Research a CPU Upgrade

Upgrade Time! Doing a lot of VMWare Workstation virtualization to create labs for self-study and training. Finding...