7 Replies Latest reply on Aug 26, 2010 4:40 AM by Marc Autret

    Access name of edittext field

    Barqx Level 1

      I created an edittext field and gave it the name "myText". This is what I tried.

       

      Window.Edit = Window.add('edittext', undefined,undefined, {name:"myText"});
      Window.Edit = func;
      
      function func()
      {
           alert(this.name);
      }
      
      

      However, I only get "undefined" as a result.

       

      How can I fix this?

        • 1. Re: Access name of edittext field
          Marijan Tompa [tomaxxi] Level 4

          What are you trying to achieve?

           

          --

          tomaxxi

          http://indisnip.wordpress.com/

          • 2. Re: Access name of edittext field
            Barqx Level 1

            I have multiple edittext fields. On entering values and clicking on a button I want the name of the textfield and the value to be read. So far I can only read the value, but not the name.

            • 3. Re: Access name of edittext field
              Marijan Tompa [tomaxxi] Level 4

              You can't retrieve ScriptUI object name of text field. Why do you need that?

               

              --

              tomaxxi

              http://indisnip.wordpress.com/

              • 4. Re: Access name of edittext field
                Barqx Level 1

                I have a number of textfields. I want to record their values when clicking on a button. So far I'm using the onChange feature. Each time the value is true, the number in the textfield is added to an array. However, I have problems, when the value changes, since then both values will be added to the array, although I just want the updated number.

                • 5. Re: Access name of edittext field
                  Harbs. Level 6

                  Don't use arrays.

                   

                  Use Javascript objects instead:

                   

                  myValues = {
                    checkBox1:checkbox1.value,
                    editText1:editText1.text,
                    editText2:editText2.text
                  }
                  

                   

                  You then simply change the values in your object:

                  editText1.onChange = function(){
                    myValues.editText1 = editText1.text;
                  }
                  

                   

                  Harbs

                  1 person found this helpful
                  • 6. Re: Access name of edittext field
                    Marc Autret Level 4
                    I created an edittext field and gave it the name "myText". This is what I tried.

                     

                     

                    Window.Edit = Window.add('edittext', undefined,undefined, {name:"myText"});
                    Window.Edit = func;
                    
                    function func()
                    {
                         alert(this.name);
                    }
                    
                    

                    However, I only get "undefined" as a result.

                     

                    How can I fix this?

                     

                    Actually there are two problems in your code:

                     

                    First, you reset Window.Edit to func, so Window.Edit is no more the EditText object you believe it is.

                     

                    Secund, name is not a construction property of EditText, so it is ignored at the widget construction time.

                     

                    However, you can add a name property after construction:

                     

                    var w = new Window('dialog', 'title');
                    w.edit = w.add('edittext');
                    
                    w.edit.name = "myText";
                    
                    // useless, but it works:
                    w.edit.getName = function(){alert(this.name);}
                    
                    // etc.
                    

                     

                    @+

                    Marc

                    • 7. Re: Access name of edittext field
                      Marc Autret Level 4

                      Barqx wrote:

                       

                      I have multiple edittext fields. On entering values and clicking on a button I want the name of the textfield and the value to be read. So far I can only read the value, but not the name.

                       

                      So, to handle the name-value pairs of multiple EditText objects, you can do something like this:

                       

                      var w = new Window('dialog', ' EditText Test'),
                           fields = ['Field1', 'Field2', 'Field3'],
                           n = fields.length,
                           i, field, t;
                      
                      w.edits = [];
                      
                      for( i=0 ; i<n ; ++i )
                           {
                           field = fields[i];
                           w.add('statictext', undefined, field + ':');
                           w.edits.push( t = w.add('edittext'));
                           t.characters = 20;
                           t.name = field;
                           }
                      
                      w.addEventListener('click', function(ev)
                           {
                           var tg = ev.target;
                           if( tg.constructor == EditText )
                                tg.text = 'My name is: ' + tg.name;
                           });
                           
                      w.add('button', undefined, 'test').onClick = function()
                           {
                           var msg = [];
                           for( i=0 ; i<n ; ++i )
                                msg.push(w.edits[i].name + ' contains: "' + w.edits[i].text + '"');
                           alert( msg.join('\r') );
                           };
                      
                      w.show();
                      

                       

                      @+

                      Marc

                      1 person found this helpful