question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Check if embla exists in ES6

See original GitHub issue

Hello, 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:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jakubreroncommented, Nov 12, 2019

It works, you are amazing!! Thank you so much!

1reaction
davidjerlekecommented, Nov 12, 2019

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:

  1. If element is found, it returns an element node.
  2. If not, it returns null.

It will never have the .length property because document.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:

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 !== null) { // change this line
  checkTestimonials();
}

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found