0

I have an input element with a value that looks like this:

value="{"logEntries":[],"value":"CHCN1","text":"CHCN AAH COMPLETE CARE (CHCN1)","enabled":true,"checkedIndices":[],"checkedItemsTextOverflows":false}"

When I retrieve the value thru jQuery all I get is the first { since its wrapped in quotes... How do I get the entire JSON object??

mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • 1
    Your input element is totally invalid, it can only have one `value` attribute – adeneo Mar 18 '15 at 21:37
  • 3
    Use single quotes around the value – Turnip Mar 18 '15 at 21:39
  • 1
    [Escaping the double quote that is in the attribute of an HTML tag](http://stackoverflow.com/questions/8007672/escaping-the-double-quote-that-is-in-the-attribute-of-an-html-tag) – Jonathan Lonowski Mar 18 '15 at 21:40
  • or assign the value in script and then set the value to JSON.stringify(object) – mplungjan Mar 18 '15 at 21:42
  • The value is created by a Telerik control. I can't change it to single quotes or otherwise change the value. – Scott Loveland Mar 18 '15 at 21:45
  • @ScottLoveland If the control is generating the markup incorrectly, it's not going to be reasonably possible to recover it after the fact. This seems like a bug in the control that Telerik would want to know about. – Jonathan Lonowski Mar 18 '15 at 21:47
  • Indeed, turns out they somehow use it, I have no Idea how but regardless I found that they have a client side method I was able to use to retrieve the value. – Scott Loveland Mar 20 '15 at 09:44

3 Answers3

0

In a node.js console, for instance, you can run JSON.parse() on a single-quoted string to turn it into a callable JSON object:

> value = JSON.parse('{"logEntries":[], "value":"CHCN1","text":"CHCN AAH COMPLETE CARE (CHCN1)","enabled":true,"checkedIndices":[],"checkedItemsTextOverflows":false}');
{ logEntries: [],
  value: 'CHCN1',
  text: 'CHCN AAH COMPLETE CARE (CHCN1)',
  enabled: true,
  checkedIndices: [],
  checkedItemsTextOverflows: false }
> value.value
'CHCN1'
> 

You would not need jQuery to parse a String into a JavaScript object.

Alex Reynolds
  • 95,983
  • 54
  • 240
  • 345
0

I assume you have the content from a server.

If you instead do

$(function() {
  var jsObj = {"logEntries":[],"value":"CHCN1","text":"CHCN AAH COMPLETE CARE (CHCN1)","enabled":true,"checkedIndices":[],"checkedItemsTextOverflows":false}
  $("#inputId").val(JSON.stringify(jsObj));
});

If not possible you need to change the input to

<input value='{....}' />
mplungjan
  • 169,008
  • 28
  • 173
  • 236
-2

The input field's value (especially the double quoutes) is not properly escaped. Escape the double quotes with a leading backslash.

nomad
  • 1
  • 2