3

This might be a stupid question, but I'm trying to pass in a string of settings I've constructed in the following format:

"setting1" : "value", "setting2" : "value", "setting3" : "value"

The above is saved to a string named args. Nothing special, but I'm wanting to pass it in as an argument to a function.

$('#element').functionName({ args });

I'm not sure what I'm missed here.... Thanks!

3
  • What exactly is args? What are you getting stuck on? Commented Feb 6, 2013 at 2:01
  • sorry, args is the var name for the string above... I'll edit Commented Feb 6, 2013 at 2:02
  • Back up a bit to where the string comes from. Makes more sense to modify it's source to create object or JSON, not a string formatted as shown Commented Feb 6, 2013 at 2:07

2 Answers 2

3

If you really have a string such as this:

'"setting1" : "value", "setting2" : "value", "setting3" : "value"'

You can parse it using JSON.parse and get an object out of it like so:

var args = JSON.parse( "{" + str + "}" );
$('#element').functionName(o);

But in reality you probably want to just create such an object instead of a string from the start, e.g.:

var args = {"setting1" : "value", "setting2" : "value", "setting3" : "value"};
$('#element').functionName(args);
Sign up to request clarification or add additional context in comments.

Comments

1

There is no such thing as a stupid question. Try:

var args = JSON.parse("{'setting1' : 'value', ...}");

And then pass the "args" variable into your function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.