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);
};
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);
};
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!
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
orpagecreate
event.Working example: http://jsfiddle.net/Gajotres/caCsf/
Example 2
Do it during the
pagebeforeshow
orpageshow
event and usetrigger('create')
to style sliders.Working example: http://jsfiddle.net/Gajotres/NwMLP/
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.