I have a jquery
variable like this :
var d1 = [{text:'t1',value:'1'},{text:'t2',value:'2'},{text:'t3',value:'3'},{text:'t4',value:'4'}];
console.log(d1);
When i display it in console.log
, i have something similar to the image below
Now if i had an input in html like this :
<input id="txtstr" name="txtstr" value="[{text:'t1',value:'1'},{text:'t2',value:'2'}, {text:'t3',value:'3'},{text:'t4',value:'4'}]" type="text" />
in console.log
i have :
console.log($("#txtstr").val());
I want to have json array in console.log
when i use input
(Like the first picture). I have also used json.parse
but it didn’t work.
3
Answers
It is because the attribute value isn’t having a value quoted, you don’t put
text
andvalue
in quotes. to fix this do the followingSo in pure JavaScript
It is because your input value does not follow the standard JSON format, where keys and string values are enclosed in double quotes.
It shoutld be
and your code will work fine