-6

for example in input name:1111,ss:1111,... => {name:1111,ss:1111,...} A method of turning characters into objects

   I can't find a good way to do it
qiang
  • 7
  • 4

2 Answers2

1

So, you need this method or what?

if need method, it's simple: just get value of textarea, wrap text into quotes, add braces and call on it JSON.parse()

var textarea = document.getElementById("id-of-textarea");
var value = textarea.value;
var wrapped = "{"+value.replace(/(\w+)/g, '"$1"') + "}";
var result = JSON.parse(wrapped);

But, you should expect any values that user can input, so it's not good idea to use textarea for input some data.

reconnect
  • 306
  • 3
  • 13
0

As per the structure of you input I would suggest you to use different textfields to capture data rather than having it into a single textarea. Like this, you will be able to iterate over those and fetch their value and store them in form of an object or a map

Your HTML:

<input id="name" value="1111"/>
<input id="ss" value="2222"/>
...

Your script (JQuery/Javascript):

var data = {};
$('input').each(function () {

data[$(this).attr("id")] = $(this).val();

});
qwerty
  • 2,392
  • 3
  • 30
  • 55