publicRavi
publicRavi

Reputation: 2763

jQuery slider ui -- dynamically set starting value from HTML element

I am trying to read the value of my slider from a DIV element's title (which may be the issue). That is pushed into the following:

$(document).ready(function() {
    $(".myslider5").slider({
        min: -1000,
        max: 1000,
        value: parseInt($(this).attr("title")),
        slide: function(event, ui) {
              // more actions
        }
    });

I think I get NaN error. Any clues?

Upvotes: 1

Views: 1663

Answers (2)

rav45
rav45

Reputation: 11

It is possible that $(this) is not taking exact value so try to give value as:

$(".myslider5").each(function() {
    $(this).slider({
        value: parseInt($(".DiVID").attr("title")),
        slide: function(event, ui) {
              // ...
              // more actions
              // ...
        },
        min: -1000,
        max: 1000
    });
});

Upvotes: 0

Nick Craver
Nick Craver

Reputation: 630379

If there's more than one of this class, you'll need to iterate over them and create the sliders using .each(), like this:

$(".myslider5").each(function() {
    $(this).slider({
        value: parseInt($(this).attr("title")),
        slide: function(event, ui) {
              // more actions
        },
        min: -1000,
        max: 1000
    });
});

Here's an example of the above, otherwise you'll get the value from the title of the first of these in the set, you can see the .attr() documentation or details:

Get the value of an attribute for the first element in the set of matched elements.

Upvotes: 2

Related Questions