Jake
Jake

Reputation: 26107

Passsing JSON in OnClick Events

How can I embed the following parameter string in an onclick event without breaking the JS? Right now there is collision with double Quotes. the values in th onclick event are coming from a JS variable.

Ex:

initializeMap('["2012-02-17 15:39:19.0,33.38727791932264,-86.74324840021933","2012-01-10 00:40:08.0,33.38708092092858,-86.74331461676397"]','%68%74%74%70%3a%2f%2f');

In an Onclick Event:

<a href="#-" onclick="JavaScript:initializeMap('["2012-02-17 15:39:19.0,33.38727791932264,-86.74324840021933","2012-01-10 00:40:08.0,33.38708092092858,-86.74331461676397"]','%68%74%74%70%3a%2f%2f');">Click Me</a>

Thanks

Upvotes: 0

Views: 1468

Answers (2)

Ruan Mendes
Ruan Mendes

Reputation: 92274

Like Jake said, this is better to handle without inline handlers. Nobody nowadays should be using inline handlers. Having said that, there are two options:

  • You can HTML escape your JS string, it would look like : "[&quot;2012-02-17 15:39:19.0,33.38727791932264,-86.74324840021933&quot;,&quot;2012-01-10 00:40:08.0,33.38708092092858,-86.74331461676397&quot;]"
  • You don't really need the quotes around the JSON, since it's valid JavaScript

Upvotes: 2

Santosh Gokak
Santosh Gokak

Reputation: 3411

I would go for a JS handler for the onclick event.

If you are stuck with inline JS, try using

<a href="#-" onclick='initializeMap(["2012-02-17 15:39:19.0,33.38727791932264,-86.74324840021933",
"2012-01-10 00:40:08.0,33.38708092092858,-86.74331461676397"],"%68%74%74%70%3a%2f%2f");'>Click Me</a>

Upvotes: 1

Related Questions