0

I've created a jQuery plugin that takes options in the form of an object. If I initialised the object client side I would do something like

$(selector).myPlugin({
    opt: value,
    id: 2,
    onclose: function () {},
});

My function would interpret the options as

1. {opt: string(value), id: int(2), onclose: function () {}}

However, the plugin declaration is made server side, and I turn a PHP associative array into the object required for the plugin options by using json_encode. The problem here is that my plugin recognises the declaration as

2. {opt: string(value), id: string(2), onclose: string(function () {})}

What I want to know is how do I pass that associative array from PHP in a way that JS interprets it as 1

4
  • Parse and pass it as JSON, this way u can convert it back and forth in both languages Commented Jul 30, 2014 at 8:43
  • @darkbee That's the problem though, I get the object in 2, which is incorrect. It interprets a function as a string Commented Jul 30, 2014 at 8:44
  • It shouldn't be encoding an integer as string from PHP. Commented Jul 30, 2014 at 8:46
  • @LukeMadhanga my bad, have to read full question next time '-_ Commented Jul 30, 2014 at 8:48

1 Answer 1

0

Cast your id value to an int before encoding the array to JSON:

$opts = array(
    'opt' => 'value',
    'id' => '2',
    'onclose' => 'function() {}'
);

$opts['id'] = (int)$opts['id'];

echo json_encode($opts);

Or even better would be if you cast the value for id before/during you construct the $opts array.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.