Search code examples
javascriptjquery-mobilejqueryjquery-mobile-slider

JQuery Mobile sliders inserted dynamically


I'm inserting sliders dynamically. The problem it's that when I insert them dynamically they don't have the theme of jquerymobile. Here's the code i use:

for (var i = array_colors_available.length - 1; i >= 0; i--) {
        $('#insert_colors_slider').append('<div data-role="fieldcontain" ><fieldset data-role="controlgroup"> <label for="slider-8">'+array_colors_available[i]+' : '+'</label><input id=slider-'+i+' type="range" name='+array_colors_available[i]+' value="0" min="0" max="25" data-highlight="true" data-theme=c data-track-theme="f"></fieldset></div>');
        if(array_slider_info_value != null) $('#slider-'+i).val(array_slider_info_value[i+1].value);
    };

enter image description here

And if I use the methods of JQueryMobile then on the screen appear two sliders:

for (var i = array_colors_available.length - 1; i >= 0; i--) {
        $('#insert_colors_slider').append('<div data-role="fieldcontain" ><fieldset data-role="controlgroup"> <label for="slider-8">'+array_colors_available[i]+' : '+'</label><input id=slider-'+i+' type="range" name='+array_colors_available[i]+' value="0" min="0" max="25" data-highlight="true" data-theme=c data-track-theme="f"></fieldset></div>');
        $('#slider-'+i).slider();
        if(array_slider_info_value != null) $('#slider-'+i).val(array_slider_info_value[i+1].value);
    };

enter image description here

What am I doing wrong? When I don't use the methods there's no theme and when I use it I have two sliders instead of one... Thanks!


Solution

  • This is just one of jQuery Mobile bugs. Also there's an error in your code, label must point to the correct slider, and in your example it is not a case.

    Dynamic slider can be created in two ways, none of them includes slider() method:

    Example 1

    Do it during the pagebeforecreate or pagecreate event.

    Working example: http://jsfiddle.net/Gajotres/caCsf/

    $(document).on('pagebeforeshow', '#index', function(){    
        // Add a new input element
        $('[data-role="content"]').append('<input type="range" name="slider-2" id="slider-2" value="25" min="0" max="100"  />');
    });
    

    Example 2

    Do it during the pagebeforeshow or pageshow event and use trigger('create') to style sliders.

    Working example: http://jsfiddle.net/Gajotres/NwMLP/

    $(document).on('pagebeforeshow', '#index', function(){    
        // Add a new input element
        $('[data-role="content"]').append('<div data-role="fieldcontain"></div>');
        $('[data-role="fieldcontain"]').append('<fieldset data-role="controlgroup"></fieldset>');
        $('[data-role="controlgroup"]').append('<label for="slider-2">Slider 2</label>');
        $('[data-role="controlgroup"]').append('<input type="range" name="slider-2" id="slider-2" value="25" min="0" max="100"  />');
        // Enhance new input element, unfortunately slider() function is not goinf to work correctly
        //$('[type="range"]').slider();
        $('#index').trigger('create');
    });
    

    In this example, if we try to use slider() only everything will be style except the input box.

    More about this and some other related stuff can be found in my other ARTICLE, or find it HERE.