Ghooti Farangi
Ghooti Farangi

Reputation: 20176

Can .ready() event be used on other tags than document

Can .ready() event be used on other tags than document? example:

$("#test").ready(function() {
  $("#test").click(function () {
    alert("test");
  });
});

Upvotes: 5

Views: 90

Answers (3)

user605334
user605334

Reputation:

in my own opinion $("#test").ready means that you check that #test is loaded in dom or not. the point @ZeSimon have that if document load then it's sure that #test is included in dom. but sometime content come from ajax request then it's not loaded in dom and not work if you call click event on them.

the length > 0 can be used to check that #test is exist instead of used ready on them because no-sense their to check using ready event.

if your #test come from ajax request then you have some option

  1. bind the click event on them

  2. use live('click') to attach a event for content [who maybe come through ajax request]

Upvotes: 1

Elad Lachmi
Elad Lachmi

Reputation: 10561

Yes, you can, but it will fire at the same time as $(document).ready() anyway, so what would be the point?

Upvotes: 0

Pascal MARTIN
Pascal MARTIN

Reputation: 401152

Quoting .ready()'s documentation's page :

The .ready() method can only be called on a jQuery object matching the current document


The selector can be omitted, but it won't change a thing : it'll still work on the document.

Upvotes: 10

Related Questions