3

Hey I have a handful of jquery sliders called "lp-slider" set up like so:

                 <div class="xs-col-12">
                        <div class="col-xs-2 label label-default">Grit</div>
                        <div class="col-xs-9">
                            <div class="lp-slider" data-lpScore="30"></div>
                        </div>
                        <div class="col-xs-1"><img class="slider-img" src="images/close-white.png"></div>
                        <br class="clear-fix">
                    </div>
                    <div class="xs-col-12 spacer-med">
                        <div class="col-xs-2 label label-default">Self-efficacy</div>
                        <div class="col-xs-9">
                            <div class="lp-slider" data-lpScore="30"></div>
                        </div>
                        <div class="col-xs-1"><img class="slider-img" src="images/close-white.png"></div>
                        <br class="clear-fix">
                    </div>
                    <div class="xs-col-12 spacer-med">
                        <div class="col-xs-2 label label-default">Sociability</div>
                        <div class="col-xs-9">
                            <div class="lp-slider" data-lpScore="30"></div>
                        </div>
                        <div class="col-xs-1"><img class="slider-img" src="images/close-white.png"></div>
                        <br class="clear-fix">
                    </div>

I'd like to run an each statement on them so that it grabs the data attribute and uses it to set the slider value. I tried this and I can't quite seem to figure out how to get it to work. It sets up the slider but value part doesn't work correctly:

$(".lp-slider").each(function() {
    var value = $(this).data("lpScore");
    $(this).slider({
        value:value,
        range:"min",
        min:1,
        max:100,
        step:1
    });
});
0

1 Answer 1

6

To get a data attribute using jQuery's data() you have to write it in lowercase like lpscore.

Here a deep explanation of the rules applied by data Using data attributes with jQuery

Code:

$(".lp-slider").each(function () {
    var value = $(this).data("lpscore");
    $(this).slider({
        value: value,
        range: "min",
        min: 1,
        max: 100,
        step: 1
    });
});

Demo: http://jsfiddle.net/IrvinDominin/G3vy6/

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

7 Comments

wow, haha that makes me want to slap myself, thanks for that!
Yes, it's a nice pitfall of jQuery data :-\
How did you arrive at that conclusion based on the linked document?
@isherwood From data-lpScore="30" and var value = $(this).data("lpScore"); <--camel case
I'm not sure how that answers my question. Anyway, I got some clarification here: stackoverflow.com/questions/7641551/…
|

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.