instance name of components created dynamically

Community Beginner ,
Nov 18, 2020

Copy link to clipboard

Copied

Hi,

i try to put 2 or more radiobuttons on the stage dynamically (but the problem is the same for any sort of component : Input text, check boxes ...).

I've made a movieClip with the radiobutton inside, with the name bouton for the component.The link to the MC in the library is Radio

So, i've displayed components with :

 

var caseH = new lib.Radio();
var caseF = new lib.Radio();

this.addChild(caseH);
this.addChild(caseF);

 

That works but, after, i try to access one of them : 

caseH = document.getElementById("bouton");
caseF = document.getElementById("bouton"); 
caseH.checked = true;

and, of course, it does'nt work cause all the instances have the same name : bouton ! So, caseH and caseF refere to the same last child added, caseF! 

How can I access to each component  instance ?

 

Thanks for your reply.

Views

74

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

1 Correct Answer

Adobe Community Professional , Nov 19, 2020
JoãoCésar Adobe Community Professional , Nov 19, 2020
Hi.   That's great!   Please try: alert("caseChg "+evt.target.id);  

Likes

Translate

Translate
Jump to answer Jump to answer
Adobe Community Professional ,
Nov 18, 2020

Copy link to clipboard

Copied

Hi.

 

As you're adding components dynamically, it will be better to rely on the ids that Animate generates automatically for each kind of component. For example:

 

RadioButton: radiobutton0, radiobutton1, and so on.

CheckBox: checkbox0, checkbox1, and so on.

 

So instead of assigning a instance name manually in the IDE, leave that field alone and just reference the components like this:

var radio0 = document.getElementById("radiobutton0");
var radio1 = document.getElementById("radiobutton1");

 

I hope it helps.

 

Regards,

JC

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
defiraid AUTHOR
Community Beginner ,
Nov 19, 2020

Copy link to clipboard

Copied

thanks Joao,

effectively, it allows me to do what i want! I did'nt know that animate calls the components with these name, by default.

And do you know how i can recover this name when an event is called.

for (i = 0; i <8; i++) {
   var caseacoche= document.getElementById("radiobutton"+i);
   caseacoche.addEventListener("change", caseChg);
}
function caseChg(evt) {
   alert("caseChg "+evt.target.name);
}

evt.target doesn't work neither this.name neither many other tryings!

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Adobe Community Professional ,
Nov 19, 2020

Copy link to clipboard

Copied

Hi.

 

That's great!

 

Please try:

alert("caseChg "+evt.target.id);

 

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
defiraid AUTHOR
Community Beginner ,
Nov 19, 2020

Copy link to clipboard

Copied

Yes, of course, the id.

i haven't thought to id !

thanks for all.

It's hard to learn again after 18 years of AS3!

Bye

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Adobe Community Professional ,
Nov 19, 2020

Copy link to clipboard

Copied

No problem! I'm sure you'll get used to Animate's HTML5 mode in no time! And the great experience you have with AS3 will help a lot.

 

Please let us know if any further questions arise.

 

Regards,

JC

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
defiraid AUTHOR LATEST
Community Beginner ,
Nov 19, 2020

Copy link to clipboard

Copied

And for those who will read this, even if you have just one instance of a component, it increase for all the components :

I explain :

var caseH = new lib.Radio();
var ...= new lib.Case();
var ...= new lib.Radio();
var ...= new lib.Texte();
var ...= new lib.Radio();
this.addChild(caseH );
this.addChild(...);
...

 Animate call them :

radiobutton0

checkbox1

radiobutton2

textinput3

radiobutton4

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Adobe Community Professional ,
Nov 18, 2020

Copy link to clipboard

Copied

or create children add one movieclip/radio button to each child.

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
defiraid AUTHOR
Community Beginner ,
Nov 19, 2020

Copy link to clipboard

Copied

thanks for your reply :

I don't understand what you mean exactly.

My components are already in a MovieClip in the Library.

You mean that instead of adding them to stage, i have to add them in one different MC each ?

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Adobe Community Professional ,
Nov 19, 2020

Copy link to clipboard

Copied

ignore my post.  it's of no help in this situation.

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more