2

I have the following code:

HTML:

 <span data-test="15, 17">

JavaScript / JQuery:

$value = [ $(object).data('test') ]

The $value variable now contains an array with one element. This element has the value "15, 17". I want to change the value of data-test in HTML in a way that $value will contain an array with two elements "15" and "17". In my environment I can only change the data-test attribute of the span. I can not change the JavaScript.

jsFiddle how the result currently looks like: ["15, 17"]

But I need it to be: ["15", "17"]

Is there any way?

Edit 1: I want to highlight the following part again: I can not change the JavaScript, only the HTML

Edit 2: I guess from the answers and comments below that with these requirements it is not possible to achieve the result I aimed for.

3
  • You can do it with javascript split() method: jsfiddle.net/3xfez/60 Commented Feb 18, 2015 at 15:24
  • 2
    If you really "can not change the JavaScript", then, no. You can't quite get the result you want. Rory's answer will get you an array-within-an-array, but that's as close as you'll get. Commented Feb 18, 2015 at 15:30
  • if you cant change the javascript, is impossible what you want. Commented Feb 18, 2015 at 15:42

3 Answers 3

2

You need to store the value as a serialised array, in other words like this:

<span data-test="[15, 17]">

When jQuery stores it in the data cache object it will be deserialised to an array:

var value = $('span').data('test'); // == Array
console.log(value.length); // = 2

Updated fiddle

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

2 Comments

Note that, for some reason, OP can change the HTML, but not the Javascript - so $value = [ $(object).data('test') ] will be run, giving [ [15, 17 ] ]
@PaulRoub good point. I would highly suggest changing the JS if possible though.
0

The right answer: is impossible what you want, if you can't change the Javascript.

However, using javascript, an way to do is:

var array = $('#demo').data('test').split(',');

Example: https://jsfiddle.net/3xfez/65/ (Don't forget to open the console.)

Comments

0

You need to store the array in JSON format.

See this fiddle: https://jsfiddle.net/3xfez/62/

The correct data value should be: [15, 17].

<span id="demo" data-test="[15, 17]"></div>

3 Comments

You're linking to the OP's original fiddle, nothing changed there. And your code here has the same issue that's leading the OP to go all boldfaced - the JS can't be changed. You'll get nested arrays.
Nested array is not possible
@PaulRoub changed the fiddle. You're right that it will be a nested array, but it is still the only way to get an array from the data attribute from the provided js.

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.