user1452062
user1452062

Reputation: 805

When I run the jQuery command the element doesn't exists

I would like to modify the dom with jquery, but when I run the command, the dom element doesn't exist. Is it possible to wait for the other script to build the necessary html elements?

I tried with the 'live' function, but doesn't help.

jQuery(document).live('ready', function(){

        var pressTitle;

        jQuery('.press-page .soliloquy-item').each(function(){

        pressTitle = jQuery(this).children().attr('title');

        console.log(pressTitle);

        });

    });

How can I fix this?

Upvotes: 0

Views: 68

Answers (1)

Guffa
Guffa

Reputation: 700442

You can check if you find any elements, and if you don't, wait for a while and try again:

function getTitle() {
  var pressTitle;
  var t = jQuery('.press-page .soliloquy-item');
  if (t.length) {
    t.each(function(){
      pressTitle = jQuery(this).children().attr('title');
      console.log(pressTitle);
    });
  } else {
    window.setTimeout(getTitle, 100);
  }
}

jQuery(document).ready(getTitle);

Upvotes: 2

Related Questions