Mg112
Mg112

Reputation: 27

JavaScript - Getting Multiline value from textarea

I wanted to get a Multiline value from textarea using js or jquery and save it to a cookie. Here's how the code looks like:

HTML:

<textarea id="cont" cols="72" rows="15">

JS:

var txt = $('#cont').val();

The variable 'txt' saves to a cookie well when it's not a multi line one, but it don't save when it has more than one line.

Here's my set cookie:

function SetCookie(cookie_name, data){
var domain = wgServer.split("//")[1];
document.cookie =
    cookie_name + "=" + data +
    "; max-age=" + 60*60*24*150 +
    "; path=/; domain=" + domain;
}  

Upvotes: 0

Views: 1033

Answers (2)

Srneczek
Srneczek

Reputation: 2193

I think you need to encode and decode those newlines with encodeURIComponent(), decodeURIComponent(), becouse you can't use some character in cookie name and/or value. It could be browser specific issue since js runs in browser and implementations can differ.

Upvotes: 0

gog
gog

Reputation: 11347

Although strictly speaking not required, it's a good practice to URL-encode cookie values to avoid surprises with special characters.

function SetCookie(cookie_name, data){
    var domain = wgServer.split("//")[1];
    document.cookie =
        cookie_name + "=" + encodeURIComponent(data) +
        "; max-age=" + 60*60*24*150 +
        "; path=/; domain=" + domain;
    }  

Similarly, use decodeURIComponent when reading it back.

Upvotes: 1

Related Questions