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?
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:
JSON.stringify
- https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/JSON/stringify
window.JSON
browser compatibility - http://caniuse.com/json
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With