user007
user007

Reputation: 3243

Get function parameter from data attribute

I am trying to get a parameter of a function from a data attribute, like there is a function which accept parameters:

$(element).transition({
    x:'100%',
    opacity: '0.5'
});

and I wan to get transition parameters from a data attribute, like:

<div class="have-data" data-transition="x:'100%',opacity: '0.5'">
    ....
</div>

Now i want to get parameters from data attribute and set it .transition() parameter to like:

.transition($('.have-data').data('transition'))

Upvotes: 0

Views: 112

Answers (1)

Olegas
Olegas

Reputation: 10517

Try the following attribute value

<div class="have-data" data-transition='{"x":"100%","opacity": "0.5"}'>
    ....
</div>

And use

.transition($('.have-data').data('transition'))

jQuery is smart and automagically converts your data from JSON to plain JS object. But note, you must use " instead of ' for quoting field names of your config-object

Upvotes: 3

Related Questions