Jump to content

G. Scott

Tweening empty jQuery elements

Warning: Please note

This thread was started before GSAP 3 was released. Some information, especially the syntax, may be out of date for GSAP 3. Please see the GSAP 3 migration guide and release notes for more information about how to update the code to GSAP 3's syntax. 

Recommended Posts



In the example below, I'm wondering if its ok if you try to tween myElement if the selector found no elements to match?

 var myElement = $(div).find('.foundit');

Typically I check to be sure the element exists with something like...

if(myElement.length > 0){...

before I would tween it. But in a project I am currently working on, I realized that I forgot to do this, yet no errors are being thrown when the element is empty. So maybe I have been adding the conditional statement unnecessarily?


Thanks for your thoughts!

Link to comment
Share on other sites

It's generally not a good idea to tween a null object, and you will get errors if you literally try to just pass null in as the target (or a variable that resolves to null), but in the case of selector text, since it still returns a wrapper of some kind (like a NodeList or jQuery object), it's technically not null so it doesn't throw errors.

Link to comment
Share on other sites

Thanks for the reply! Yes, thats what I suspected. I'll continue checking since I think thats really the correct way to go... even though it doesn't break anything if I don't. As always, GSAP rocks! THANKS!

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.