Jump to content


Draggable - changing CSS properties onClick

Go to solution Solved by PointC,

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



I'm learning how to use Draggable.

I have some divs styled as coloured boxes with some basic text in.

<div id="answer1" class="box-answer"><span class="text">a ratio</span></div>

I use the following Javascript to facilitate the div being Draggable and then to change the colour of the box when it receives an onClick. I'm not sure why, but it doesn't seem to work

Draggable.create(".box-answer", {
	bounds: "#container",
	onClick:function() {
		console.log("clicked", this.target.id );
		$( "#"+this.target.id ).css("background-color","#333333");
	onDragEnd:function() {
		console.log("drag ended");

If anyone can show me how I change the CSS properties of a Draggable element after receiving a mouseEvent I'd be very grateful.


Many thanks in advance







Link to comment
Share on other sites

  • Solution

Hi there d11202 :),


You didn't have a CodePen so I made a really quick one using your HTML above.


See the Pen qOpeEV by PointC (@PointC) on CodePen


I set the color change for a dragStart, but you can also set it for a click. 


I hope this helps.


PS For future reference - a CodePen is usually the fastest way for others to help with your questions. Check out this post on how to make one:


  • Like 3
Link to comment
Share on other sites

Hi PointC


Thank you very much for your help.



Link to comment
Share on other sites

Nice job PointC :)

Link to comment
Share on other sites

Hi PointC


...just one last thing. Are you or anyone else able to say why the jQuery reference in my question would not change the CSS value of the Draggable element?

onClick:function() {
	console.log("clicked", this.target.id );
        $( "#"+this.target.id ).css("background-color","#333333");
Link to comment
Share on other sites

Perhaps it's because you're not accessing the ID properly - that's an attribute, so you'd need to use getAttribute() like:

$( "#"+this.target.getAttribute("id") ).css("background-color","#333333");

But actually, this would probably be cleaner:


Or even: 

this.target.style.backgroundColor = "#333333";
  • Like 4
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.