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?
data-texts=" 'a', 'b', 'c' "
format instead ofdata-texts=' "a", "b", "c" '
? – Whitish