Check if embla exists in ES6
See original GitHub issueHello, i have a problem with checking if element exists and initializing the Embla carousel.
I want to do something like: If element exists on the page, then initialize it with Embla carousel, because when i do it without checking the element first, then on another subpage, i get an error saying “Error: No root element provided 😢”
My code looks like that:
import EmblaCarousel from 'embla-carousel';
const testimonials = document.querySelector('.embla__testimonials');
const checkTestimonials = () => {
return EmblaCarousel(testimonials, {
align: 'center',
containerSelector: '*',
slidesToScroll: 1,
containScroll: false,
draggable: true,
dragFree: false,
loop: false,
speed: 10,
startIndex: 0,
selectedClass: 'is-selected',
draggableClass: 'is-draggable',
draggingClass: 'is-dragging',
});
}
if (testimonials.length) {
checkTestimonials();
}
The code above doesn’t work, it checks correctly for the element, but does not initialize the carousel, and i need to initialize it always after checking the element first 😕
Does anyone know how i can solve it? Thanks!
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
3 Ways to Check If an Object Has a Property in JavaScript
There are mainly 3 ways to check if the property exists. The first way is to invoke object.hasOwnProperty(propName) . The method returns true...
Read more >Check if Object is Empty in ES6 [duplicate] - Stack Overflow
You can do this way. const checkIfVerifiedExists = (user) => { if (user && user.verified && Object.keys(user.verified).length) { return true ...
Read more >3 Ways to Check If a Property Exists in an Object in JavaScript
How to check if a property exists in an object in JavaScript by using the hasOwnProperty() method, the in operator, and comparing with...
Read more >4 Ways to Check if the Property Exists in JavaScript Object
The most common solution would be to use hasOwnProperty() which is one of the common object methods. This method returns a boolean indicating ......
Read more >sorrycc/awesome-javascript - GitHub
A collection of awesome browser-side JavaScript libraries, resources and shiny things. - GitHub - sorrycc/awesome-javascript: A collection of awesome ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
It works, you are amazing!! Thank you so much!
Hi Jakub (@jakubreron),
Thank you for the additional code and information. I see one issue with the JavaScript code. The
document.querySelector()
returns two possible values:element node
.null
.It will never have the
.length
property becausedocument.querySelector()
will not look for a list of elements but rather a single element. So your if statement will always be falsy and never run the code inside it.A solution for this is checking for
null
like so:You can read more about
document.querySelector()
on this page.I hope this helps, and would very much appreciate if you could confirm if this is working or not.
Best, David