2

I'm writing a javascript animation script and I want people to be able to specify behaviour declaratively in the HTML.

This is what I want my HTML to look like:

<div data-animation="top: 600, left: 600, opacity: 1, start: 0.2, finish: 0.8"></div>

This obviously isn't valid JSON, but you can't write valid JSON in HTML. Is there a way to turn this into JSON?

I want to be able to get the attribute value and turn it straight into a javascript object that I can use without having to manually parse the string. I.e. I want this:

var example = {
    top: 600,
    left: 600,
    opacity: 1,
    start: 0.2,
    finish: 0.8
};
2
  • did you try data-animation="{top: 600, left: 600, opacity: 1, start: 0.2, finish: 0.8}" Commented Jun 1, 2015 at 14:31
  • might help you jsfiddle.net/satpalsingh/un0domue Commented Jun 1, 2015 at 14:37

5 Answers 5

1

A more manual approch :

function getParametters(divId)
{
    var retObj = {}
    jQuery.each($("#" + divId).attr("data-animation").split(","),
    function(index, value){
        var spliced = value.split(":");
        retObj[spliced[0].trim()] = spliced[1].trim();
    });
    return retObj;
}
Sign up to request clarification or add additional context in comments.

1 Comment

I was trying to avoid manual parsing but having seen the alternatives I accept that this is the best solution.
1

You could use eval().

The eval() method evaluates JavaScript code represented as a string.

See the following

var animationString = $('div').data('animation');
eval('({' + animationString + '})');

Run this in your console to see how it works:

eval('({top: 600, left: 600, opacity: 1, start: 0.2, finish: 0.8})');

The above code returns the following Object:

Object {top: 600, left: 600, opacity: 1, start: 0.2, finish: 0.8}

Hope that helps

4 Comments

Nicely done. Do you mind explaining why you gotta put parentheses inside the string?
Read this stackoverflow.com/questions/197769/… . Code injection should not be a problem in your case and the performance hit you might take should be very minimal and hardly noticeable, but ultimately its up to you whether you use it or not.
Agreed, your link makes some good arguments. However, this code might be used by other developers and I don't want to give the impression that eval is generally okay to use.
No worries, would have chosen the other solution myself. Cheers
1

You can, using JSON.parse():

$('div[data-animation]').each(function() {
    var json = JSON.parse($(this).attr('data-animation'));
});

See this fiddle.

2 Comments

This is the simplest and best answer. JSON.parse() provides the exact functionality the OP requested.
This doesn't fulfil the exact functionality because it requires the use to single quotes for the HTML and then valid JSON written into the attribute. This is different to the HTML posted in the original question, part of the problem being that it isn't in a format that JSON.parse will handle.
0

Try this:

$(function(){
   var a = $("div").data("animation");
   var b = JSON.stringify(a);
   var obj = JSON.parse(b);

   console.log(obj);
});

1 Comment

your answer is too close.
0

You can add a class or id to the div and access the attribute value and store in to a object like below.

var animation=$('.classname').attr('data-animation');

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.