Adding jQuery UI slider to dynamically generated element?

So, I am adding list items to the list using .append (). In the added div element, I need to attach a jQuery Slider widget. Not sure if I need to use .on () or something like that. FWIW, you can add an unlimited number of li, so I use the class for div.

Anyway, here's a simplified snippet:

$('.cycleDuration').slider(); $cycleBlock += '<li>'; $cycleBlock += '<div class="cycleDuration"></div>'; $cycleBlock += '</li>'; $('#cycles').append($cycleBlock); 
+4
source share
2 answers

You will need to bind the code before the item is actually added. I think. In this example, I just linked the click event because I don't have a slider code.

http://jsfiddle.net/4vwUd/1

 $('button').click( function() { //turn your div into a jquery object var $cycleBlock = $('<div class="cycleDuration"></div>'); //bind the event $cycleBlock.bind('click', function() { alert(); }); //append to the list $('#cycles').append('<li />').children('li:last').append($cycleBlock); }); 
+4
source

just u can re-call "$ ('. cycleDuration'). slider ();" after each addition of list elements that will bind the added class elements to this function.

+2
source

Source: https://habr.com/ru/post/1389384/


All Articles