seiyria / bootstrap-slider

A slider control for Bootstrap 3 & 4.
http://seiyria.github.io/bootstrap-slider/
Other
3k stars 1.14k forks source link

$(el).slider() returns undefined if target element does not exist in DOM #916

Closed ollyollyollyltd closed 5 years ago

ollyollyollyltd commented 5 years ago

Fiddle: https://jsfiddle.net/aduw6L0p/

Bootstrap Slider v10.6.1 Jquery v3.3.1 Tested using Chrome v72

Description If you call slider() on a jQuery object which has matched no elements, it will return undefined which will break chaining.

Steps to reproduce

Expected behaviour The return value should be an empty jQuery object, the same as if you had called $(). This would allow safe chaining with further jQuery functions such as on() regardless of whether the element has been matched or not.

Actual behaviour The return value is undefined and calling other jQuery functions will throw an error. The original object is unchanged (i.e. what you called .slider() on is unchanged.

ollyollyollyltd commented 5 years ago

Should note - as a workaround (as demonstrated in the fiddle) you can still safely attach events to the 'original' object without chaining them:

const $el = $('#iDontExist');
$el.slider() // returns undefined
$el.on('slide', handleSlide);
// Works fine, whether or not `#iDontExist` is present in DOM.
$('#iDontExist').slider().on('slide', handleSlide);
// Breaks if `#iDontExist` is not present in DOM as slider() will return undefined.
ollyollyollyltd commented 5 years ago

Upon investigation this may be an error with Bridget implementation in bootstrap-slider.

I'm not sure how you handle this dependency as it is bundled into the main src/bootstrap-slider.js file. I've created a pull request which fixes the issue: https://github.com/seiyria/bootstrap-slider/pull/917

jespirit commented 5 years ago

Issue fixed. Closing.