Okky
Okky

Reputation: 10466

Save textarea value to JSON

How to save the value of textarea to JSON?

I have a textarea; I need to save value of that text area into a js object.

Its working fine if there is no 'enter'(line break) pressed. How to save it and and retrieve it as such with linebreaks.

Save using

dataObject[0]["Category"][0]["Category"] = $('textarea').val();

Upvotes: 3

Views: 9862

Answers (2)

Okky
Okky

Reputation: 10466

Used this code to make it HTML

var newText = $('textarea').val(); //value
newText = newText.replace(/\r?\n/g, '<br />');

Upvotes: 6

Vahid Taghizadeh
Vahid Taghizadeh

Reputation: 997

i think this code can help you !

$(document).ready(function(){
var textarea = $('textarea').val();
var linebreak = textarea.split('\n');
var length = linebreak.length;
var data = [];
for ( var i = 0 ; i<length ; i++){
    data.push({ 'line': i , 'content': linebreak[i] });
    console.log(data);
}

});

for more test you can go here : http://jsfiddle.net/ABy4j/10/

Upvotes: 1

Related Questions