4

Say I have a hash and I want to enter it as a val()

$("#form_attribute").val( hash )

It gets stored as a string "[Object, object]"

How do I keep it as a hash and then allow the form to send this hash to my server?

Trip
  • 26,756
  • 46
  • 158
  • 277
  • I'm pretty sure you'd have to serialize it, before storing it in the `input` element. – BenM Dec 10 '12 at 19:24
  • Is `hash` an object of some sort, or is it an actual string? If it an object, you could try the `JSON.stringify` method to convert the object to a string. On the server, you will have to turn it back into an object. – Kyle Dec 10 '12 at 19:24
  • By "*hash*" you mean "object"? Please show us what exactly you have there. – Bergi Dec 10 '12 at 19:24

2 Answers2

9

If you want to convert an object/value to a JSON string, you could use JSON.stringify to do something like this:

$("#form_attribute").val(JSON.stringify(hash))

This is a built-in method to most recent browsers that converts an object to JSON notation representing it. If a certain browser doesn't support it, there are several polyfills to include on your page to provide support


References:

Community
  • 1
  • 1
Ian
  • 50,146
  • 13
  • 101
  • 111
  • I haven't tried posting a json string server side from an input text box which is why I asked. Sounds interesting. – Travis J Dec 10 '12 at 19:28
  • This is new to me as well, I tested out the syntax here: http://jsfiddle.net/dboots/gqXmZ/. Well done! – Don Boots Dec 10 '12 at 19:29
  • 1
    @TravisJ Ahh okay. Well, there's no reason why it shouldn't work - `JSON.stringify` simply turns an object literal or similarly "valid" JSON into a string...and all strings can be submitted to the server :) Now, I'm not sure why you'd want to submit a full object literal, but that's not my problem! – Ian Dec 10 '12 at 19:30
  • @Ian - Good point. I also out of curiosity looked up what the default max length for a text input is, and apparently it is unlimited so in theory this should even work for very complex json data. – Travis J Dec 10 '12 at 19:36
  • @TravisJ Didn't think of that, thanks for looking. I think the one limitation to consider is the max size of GET requests, which I would hope is not being used for this anyways. POST would be fine though – Ian Dec 10 '12 at 19:41
  • @Ian - That is true. For POST it is huge, on average 2GB. But safari, ie, and some other browsers start at 4KB for a GET request. – Travis J Dec 10 '12 at 19:42
  • 1
    @TravisJ Also, I'm not sure this is the correct way to be handling this "hash", so this was just my quick solution for the OP wanting to store it in an input field, for whatever reason – Ian Dec 10 '12 at 19:42
  • @TravisJ Exactly, so whichever's being used, it's not really affected by the use of `JSON.stringify` - if the OP needs to send it to the server, the limit of the request is the bottleneck – Ian Dec 10 '12 at 19:45
4

You can store it as a JSON string:

$('#form_attribute').val(JSON.stringify(hash));

Or you can store your original object in a data attribute:

$('#form_attribute').data('hash', hash);
Blender
  • 289,723
  • 53
  • 439
  • 496
  • 2
    I think it's important to note that storing it in `.data` doesn't automatically submit it with a normal form submission like `.val` does – Ian Dec 10 '12 at 19:29