Score:-1

jQuery selector does not find the view content after document.ready event

za flag

I am applying some jQuery to make a slick carousel from a Drupal view.
The carrousel does not work erratically on page load; sometimes, it does, sometime it doesn't.

jQuery(($) => {
  $(document).ready(() => {
    console.log('ready');
    if ($('.view-carrousel .view-content').length) {
      console.log('content?'); //enters here erratically
    }
  }
}

Do you have any explanation?

Is it cache bubbling? I added {{ content|without('field_article_visuel', 'links') }} to the template.

Is it loading order? I added the following lines.

js/amu_slider.js: {}
dependencies:
  - core/views.module
ru flag
You have to create a Javascript "behavior" for exactly the problems you descibed here. See https://www.drupal.org/docs/drupal-apis/javascript-api/javascript-api-overview
id flag
I agree - this code isn't using Drupal's JavaScript API.
apaderno avatar
us flag
Are you using `core/views.module` as dependency in a .libraries.yml file?
Matoeil avatar
za flag
@apaderno yes; bad idea ? i mean, i have corrected it to views/views.module, intending to delay the JS after everything's being loaded from the view
apaderno avatar
us flag
It just does not do what you probably think. Drupal core expects it to be a library name, not a module name. for example, *core/drupal* is the *drupal* library defined in the [core.libraries.yml](https://api.drupal.org/api/drupal/core%21core.libraries.yml/9) file.
Matoeil avatar
za flag
right but views/views.module is a library , no?
apaderno avatar
us flag
*views/views.module* is a library. Since you used *core/views.module*, it seemed you added a module as dependency.
Matoeil avatar
za flag
The bug would be coming from the module big pipe, and particularely when connected. Anyone experienced such problem?
Score:-1
za flag

meanwhile this seems to fix it

js/amu_slider.js: {}
  dependencies:
    - views/views.module
apaderno avatar
us flag
*views/views.module* is the correct library name, since it is the *views.module* library defined in the [views.libraries.yml](https://api.drupal.org/api/drupal/core%21modules%21views%21views.libraries.yml/9) file.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.