Skip to main content
Known Participant
November 18, 2020
Answered

instance name of components created dynamically

  • November 18, 2020
  • 2 replies
  • 695 views

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.

    This topic has been closed for replies.
    Correct answer JoãoCésar17023019

    Hi.

     

    That's great!

     

    Please try:

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

     

    2 replies

    kglad
    Community Expert
    Community Expert
    November 18, 2020

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

    defiraidAuthor
    Known Participant
    November 19, 2020

    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 ?

    kglad
    Community Expert
    Community Expert
    November 19, 2020

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

    JoãoCésar17023019
    Community Expert
    Community Expert
    November 18, 2020

    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

    defiraidAuthor
    Known Participant
    November 19, 2020

    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!

    JoãoCésar17023019
    Community Expert
    JoãoCésar17023019Community ExpertCorrect answer
    Community Expert
    November 19, 2020

    Hi.

     

    That's great!

     

    Please try:

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