2

First, I created a virtual carousel (carousel elements) and then initialize it

$('#first-carousel').carousel();

Now, I run unto scenario where if window width is less than 480 then remove that carousel (#first-carousel').

How to remove bootstrap carousel? Tried to check its doc but seems there's no something like destroy or remove function or any related. Tried

$('#first-carousel').remove();
$('#first-carousel').unbind();

but seems not working, instead it gives me this error

bootstrap.min.js:6 Uncaught TypeError: Cannot read property 'offsetWidth' of undefined at c.slide (bootstrap.min.js:6) at c.next (bootstrap.min.js:6) at e (jquery.min.js:2)

any help, ideas?

Juliver Galleto
  • 8,831
  • 27
  • 86
  • 164

2 Answers2

1

Apparently there are no way to do it in Boostrap 3. What you can do is only initialize the carousel if the viewport is greater or equal to 480px.

if(window.innerWidth >= 480){
  $('#first-carousel').carousel();
}
Pablo
  • 2,137
  • 1
  • 17
  • 16
0

The code $('#first-carousel').remove(); does remove the element with that given id. You might be having an issue with the condition for window width possibly, are you sure you aren't having a different issue here?

C Y
  • 26
  • 5
  • 1
    Again, tried .remove() but it gives me this error "bootstrap.min.js:6 Uncaught TypeError: Cannot read property 'offsetWidth' of undefined at c.slide (bootstrap.min.js:6) at c.next (bootstrap.min.js:6) at e (jquery.min.js:2)" – Juliver Galleto Aug 14 '17 at 09:40
  • The given method would not be returning the error you are receiving, which is why I ask if you could maybe check or provide where you are actually getting this error. I have added a carousel with the same id as you have shown, removed it with .remove() and it causes no errors. The element is actually removed from the html fully. – C Y Aug 14 '17 at 09:42
  • I tried it with the latest cdn from bootstrap site as well on this: https://jsfiddle.net/4qk0thx3/ -- Works just as my local attempt, with an actual carousel I was using in one of my sites. – C Y Aug 14 '17 at 09:58