Jump to content
GreenSock

mimeartist

kill(), but check the instance exists first?

Go to solution Solved by Jonathan,

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

Is there a way of checking a draggable instance of a div exists?

 

I'm using kill() for something but it is throwing an error if I try and kill something that's not there, so my question is, can i check for the draggable without getting an error?

Link to comment
Share on other sites

Hello..

Did you mean check if your element exists before killing it?

You could do something like this to check if element exists first
 

var $element = document.getElementById("box");

if($element.length > 0){
    // place kill() code here
    console.log("#box exists in DOM");
}

You would use the length property

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length

Hope this helps!

  • Like 2
Link to comment
Share on other sites

Thanks Jonathan,

 

it's not the element I need to check for, it's whether it has a draggable assigned to it... ideally I'd keep a track on them all but hoping there is a simple way to just check if the draggable exists first as it's not a certainty that some items are draggable.

Link to comment
Share on other sites

  • Solution

Im sure Jack and Carl could give a better answer regarding checking if your Draggable instance exists.

 

But in the meantime .. Have you tried checking if it is an object:

:

var myDraggable;

// instance does not exist - outputs 'undefined' 
console.log(myDraggable);

// create GSAP Draggable instance
myDraggable = Draggable.create("#yourID", {
	type:"y"
});

// do some code stuff here

// check if myDraggable instance is an object
if(typeof myDraggable === 'object'){
    // instance exists
    console.log("myDraggable instance exists");
}

:

If this doesn't help could you please provide a limited example of what your are seeing in a codepen example. This way we can better help you by seeing your code in context.

 

How to setup a codepen example.

 

Thanks!

  • Like 2
Link to comment
Share on other sites

I'm such an idiot... I'd done this... and it wasn't working... turned out I was typing 'underfined'

 

if (typeof Draggable.get($(tempClip))!='undefined'){

Draggable.get($(tempClip)).kill();
}
  • Like 2
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.
×