0

I am trying to create a script to manipulate the DOM in Wordpress. My problem is that when creating the variables they appear empty because my script loads before the elements exist. How can I solve this? I have this little test that although it says the element exists, when I check the variable in the console it returns 0.

Many thanks.

var el2 = jQuery(".slick-slide");

if (el2 > 0) {
  console.log('element does NOT exist in DOM');
} else {

  console.log('element exists in DOM');
}

2 Answers2

0

In jQuery, you do this like

$(function() {
var el2 = jQuery(".slick-slide");

if (el2 > 0) {
  console.log('element does NOT exist in DOM');
} else {

  console.log('element exists in DOM');
}
});
ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
  • still no luck :( – user12487679 Aug 17 '22 at 15:25
  • Not adding the div jQuery(function() { var slides = jQuery(".slick-slide").length - jQuery(".slick-cloned").length var columnsWidth = (100 / slides)-1.7 var a = "" var last = slides - 1 for (var i = 0; i < slides ; i++) { a += jQuery(".para-testar").append('
    ') } });
    – user12487679 Aug 17 '22 at 15:26
0

Try create variables when document is ready

$( document ).ready(function() {
    var el2 = jQuery(".slick-slide");

....