Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ollyollyollyltd opened this issue Mar 12, 2019 · 3 comments

Comments

@ollyollyollyltd
Copy link
Contributor

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

  • Call slider() on a jQuery object which contains no elements
  • Examine the returned value

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
Copy link
Contributor Author

ollyollyollyltd commented Mar 12, 2019

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
Copy link
Contributor Author

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: #917

@jespirit
Copy link
Collaborator

Issue fixed. Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants