praks5432
praks5432

Reputation: 7792

Jquery Selector Returning Null on Userscript

I have an userscript running on chrome. I think jquery has been loaded because I used

// @require     http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js 

I am sure on the page that a table element exists and I've checked this line -

$('table')

In the console, and it gives back an array of tables. However, when I put this line in my userscript and log that into console, it returns a [].

What's going on?

Upvotes: 1

Views: 914

Answers (1)

gen_Eric
gen_Eric

Reputation: 227240

Your code was returning [] because it was being ran before the DOM was ready. You need to wrap your code inside a $(document).ready(function(){}) ($(function(){}) is shorthand).

$(function(){
    console.log($('table'));
});

Upvotes: 4

Related Questions