Jump to content
Search Community

exchange elements with ID

joachim test
Moderator Tag

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

Hi,

 

is there a way to preserve all an elements attributes and just exchange the Ids ?

 

I translate and rotate both g collections and at some point I want to exchange ONLY the elements Ids,

 

0g becomes 1g and 1g becomes 0g, how can I do this ?

 

This does not work:

 

exchangeElms(gArray[0].id, gArray[1].id);

 

function exchangeElms(param1, param2){
      var temp = param1;
      param1 = param2;
      param2 = temp;
    };

 

<svg .......

<g id= "0g">

     <rect class="base bring2" x=  "0" y=  "0" /> 

     <rect class="dring2" x= "25" y= "25" width= "75" height= "75" />

</g>

<g id= "1g">

     <rect class="base bring2" x="100" y=  "0" /> 

     <rect class="dring2" x="100" y= "25" width="100" height= "75" />

</g>

</svg>

Link to comment
Share on other sites

Your function isn't doing anything because you're passing it strings. When you pass a primitive like a number or string to a function, it copies it, so changes made inside the function will have no effect outside the function. That's the same reason you can't tween a number or a string.

var word = "foo";
var num  = 7;
TweenLite.to(word, 1, { ??? });
TweenLite.to(num,  1, { ??? });

Here's an easy read that explains the differences between references and values.

https://snook.ca/archives/javascript/javascript_pass/

 

So what's the fix to your problem? Just pass in the elements.

function exchangeElms(element1, element2){
  var temp = element1.id;
  element1.id = element2.id;
  element2.id = temp;
}
  • 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.
×
×
  • Create New...