0

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'))

1 Answer 1

3

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

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

6 Comments

Please note: I am creating my data attributes using attr,
I am having problem, As I am creating data attribute using attr I cant use ', $(elem).attr('data-transition', input.value) I am using attr cause I am saving HTML to database. Any suggestion please
I will use this in data attribute: x:100%|opacity:0.5 and later I will use .replace(). Done
@user007 why are you using attr() to create data attributes? Use data() as that's what it was intended for.
Already told, if I create data using data() then it will not visible in DOM, so if you save HTML you will not get data
|

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.