2

I need to pass this array into a function:

[
['ib-marker1', -24.9256,-43.8994, 0],
['ib-marker2', -22.9556,-43.8994, 0]
]

The array needs to be embedded in the HTML, so I added it like so:

<div id="map" data-markers="[['ib-marker1', -24.9256,-43.8994, 0],['ib-marker2', -22.9556,-43.8994, 0]]">

Then I take that info and call my function:

var markers = $(this).find("#map").data("markers"); init(markers);

This is a Google Maps function. No marker is recognized. I gather it is because my markers variable contains a string, not a real array. I just can't figure out how to do it.

As a test, I tried passing the array directly through the variable (not in the data attribute) and it worked fine:

var pins = [['ib-marker1', -24.9256,-43.8994, 0],['ib-marker2',
-22.9556,-43.8994, 0]];

Can someone please help me out? Thanks!

5
  • 1
    var markers = eval($(this).find("#map").data("markers")); Commented May 27, 2014 at 18:24
  • 1
    avoid eval() and try JSON.parse() on the data-markers content Commented May 27, 2014 at 18:31
  • An HTML attribute is a string. Your pins array is a javascript array, the are not the same. You need to write code to parse the string to create the array with the data you want in it. Commented May 27, 2014 at 18:34
  • JSON.parse didn't work, but eval did! Why should I avoid eval()? Anyway, if you post this on an actual answer, I´ll accept it. Thanks a lot guys! Commented May 27, 2014 at 18:35
  • If you can alter the syntax of your data parameter, such as replace the braces with a custom character that doesn't occur elsewhere, you could use split to make an array of your points as strings and then run array.forEach to further turn each element into a small nested array. Commented May 27, 2014 at 20:13

2 Answers 2

1

Considering nobody posted the right answer as an actual answer, I'm posting dfsq and Ayyash's answers here. This actually did the trick:

var markers = eval($(this).find("#map").data("markers"));
Sign up to request clarification or add additional context in comments.

2 Comments

eval-ing a user-accessible element data? A possible attack would be i.e: someone accessing or appending a #map and modifying the data-markers on your page. Injecting into data a malicious JS string. Your code will than process that JS. How cool is that?
It's a good point. What would you suggest doing instead? @RokoC.Buljan
0

You must use valid JSON(not an object-literal), then jQuery may parse the data correctly via .data("markers").

Valid JSON uses double-quotes to enclose strings(not single-quotes):

<div id="map" data-markers="[[&quot;ib-marker1&quot;,-24.9256,-43.8994,0],[&quot;ib-marker2&quot;,-22.9556,-43.8994,0]]"></div>

http://jsfiddle.net/cnG4b/ (take a look into the console)

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.