skip to Main Content

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

Answers


  1. 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');
    }
    });
    
    Login or Signup to reply.
  2. Try create variables when document is ready

    $( document ).ready(function() {
        var el2 = jQuery(".slick-slide");
    
    ....
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search