0

I try to modify some HTML5 data-attribute with jquery.

I know how to do when it is simple like this :

<div id="element" data-options="HelloWorld"></div>

//Modify with jQuery :
$("#element").data("options","Bye Bye");

But in my case, i would like to modify a more complexe data-options (it's a joomla module).

data-options is organized like this with an array of datas :

data-options="{"title":"Test","lat":"48.6069129","lng":"7.7612831","icon":"red","popup":1},{"title":"Test","lat":"48.6069129","lng":"7.7612831","icon":"blue","popup":2}"

How can i select and modify only icon for example ?

3
  • You will simply need to convert it from a string to an object using JSON parsing. The double-quotes are going to cause you grief though. Commented Jan 14, 2015 at 10:42
  • Ensure that you use the correct quotes, as it stands the value is invalid. Commented Jan 14, 2015 at 10:43
  • How will the JSON literal be injected into the elements? Is that server-side (if so what technology are you using server-side)? Commented Jan 14, 2015 at 10:54

2 Answers 2

2

You can use the function overload of jQuery.fn.data

$('#element').data('options', function(data){
   var obj = JSON.parse(data);
   obj.forEach(function(o){
       o.icon = "some other color";
   });
   return JSON.stringify(obj);
});

The above works assuming you have the following as your data.

'[{"title":"Test","lat":"48.6069129","lng":"7.7612831","icon":"red","popup":1},{"title":"Test","lat":"48.6069129","lng":"7.7612831","icon":"blue","popup":2}]'

To properly declare the json, you can put 'em in '' instead of "" which will save you from the headache of escaping double quotes.

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

Comments

1

From comment: You will simply need to convert it from a string to an object using JSON parsing. The double-quotes are going to cause you grief though.

 var values = $.parseJSON($('#element').data('options'));

One only way to include a JSON literal in a standard HTML attribute is by encoding the "'s to &quot;s which is pretty horrible:

e.g.

data-options=";{&quot;title&quot;:&quot;Test&quot;,&quot;lat&quot;:&quot;48.6069129&quot;,&quot;lng&quot;:&quot;7.7612831&quot;,&quot;icon&quot;:&quot;red&quot;,&quot;popup&quot;:1},{&quot;title&quot;:&quot;Test&quot;,&quot;lat&quot;:&quot;48.6069129&quot;,&quot;lng&quot;:&quot;7.7612831&quot;,&quot;icon&quot;:&quot;blue&quot;,&quot;popup&quot;:2}";

or you can use single quotes for the delimiting (while not standard this will work on most browsers):

data-options='{"title":"Test","lat":"48.6069129","lng":"7.7612831","icon":"red","popup":1},{"title":"Test","lat":"48.6069129","lng":"7.7612831","icon":"blue","popup":2}'

5 Comments

Close enough but <a data-options='json string can go here'></a>. Note that I've used a ' instead of "
@Amit Joki: Noted with disclaimer (as that is non-standard and sometimes not possible depending on the tech used).
I wanted to ask you to clarify your comment about the quote. Thank you for your answer, now, i understand. I didn’t code this Joomla module (widgetkit map). But I can easily replace double quote by single quote.
@Sébastien Gicquel: In that case single quotes will do: w3.org/TR/html4/intro/sgmltut.html#h-3.2.2
I'm ok with the &quote format it works jsfiddle.net/estuqq26/1 and if used in Rails application ERB is rendering it like that (eq8.eu/blogs/12-json-array-in-html-element-data-attribute)

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.