11

I have this HTML:

<section id="SSID" data-texts="'Text1', 'Text2', 'Text3'"></section>

I want to create an Array variable in jQuery and my jQuery code is:

$(document).ready(function() {
    var Selection = $("#SSID").data("texts");
    var Texts = [ Selection ];

    console.log(Texts.length);
});

For my example, the result I expect is:

Texts[0] = 'Text1'
Texts[1] = 'Text2'
Texts[2] = 'Text3'

...and that the length of the array Texts is 3.

However, what I am seeing is that the length of Texts is 1 and that the entire string is being loaded into Texts[0]:

Texts[0] = "'Text1', 'Text2', 'Text3'"

I think my problem is being caused by the " (quotation mark) characters. How can overcome this problem and achieve my objective?

2 Answers 2

20

data- attributes can contain JSON.

jQuery will automatically parse them for you, if they are syntactically valid.

<section id="SSID" data-texts='["Text1", "Text2", "Text3"]'></section>

and

$(function() {
    var texts = $("#SSID").data("texts");

    console.log(texts.length);  // logs "3"
});

See: http://jsfiddle.net/5mtre/


Security hint: You must encode the JSON correctly on the server.

This means that you need to do JSON encoding and HTML encoding, here shown examplary using PHP:

<section id="SSID" data-texts="<?=htmlspecialchars(json_encode($data), ENT_QUOTES, 'UTF-8')?>"></section>
Sign up to request clarification or add additional context in comments.

10 Comments

You should also add flags and encoding, see this answer. Fixed my issue stackoverflow.com/a/8832570/1643487
@Tim Just for reference, can you shed light on the circumstances under which omitting ENT_QUOTES, 'UTF-8' would lead to wrong results?
when you have a single qoute in your JSON data, for example: data-texts='["Text1", "'Text2", "Text3"]'. This wil break at 'Text2.
You can also just do json_encode($data, JSON_HEX_QUOT) to change quotes to &quot;.
@Nicholas No, that is not enough. All this does is encode the JSON itself differently. {"key": "value with \"quotes\"..."} becomes {"key": "value with \u0022quotes\u0022..."}, which is technically the same thing. But the JSON string will still contain <, >, & and " unencoded and this will break the HTML. Preventing to HTML breakage is the whole point of htmlspecialchars(). General advice: Don't try to be smart about this kind of thing. Always use the proper functions. Rigorously encode for the medium you are using, layer by layer, in this case: Raw Data -> JSON -> HTML.
|
8

You can use JSON.parse()

HTML :

<section id="SSID" data-texts='"Text1", "Text2", "Text3"'></section>

JQUERY :

var Selection = JSON.parse('[' + $("#SSID").data("texts") + ']');

Fiddle Demo

or

HTML :

<section id="SSID" data-texts='"Text1", "Text2", "Text3"'></section>

JQUERY :

var Selection = JSON.parse($("#SSID").data("texts"));

FYI : But it would be better to store the actual JSON as the data attribute value. For eg : data-texts='["Text1", "Text2", "Text3"]' and parse it directly.


UPDATE : Or you can do it using Array#map method and String#split method.

var Selection = $("#SSID").data("texts").split(',').map(JSON.parse);

console.log(Selection);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<section id="SSID" data-texts='"Text1", "Text2", "Text3"'></section>

2 Comments

Is there any way to use data-texts=" 'a', 'b', 'c' " format instead of data-texts=' "a", "b", "c" '?
@Phoenix Note that this is the technically inferior solution. Use actual JSON, not some string with manually added square brackets.

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.