8 Replies Latest reply on Feb 14, 2013 9:33 AM by pkrk

    Radiobutton

    pkrk Level 1

      Can the onclick function be used on a radiobutton to "asynchronously" update the text in a edittext box?

       

      With no button involved?

        • 1. Re: Radiobutton
          Peter Kahrel Adobe Community Professional & MVP

          Sure:

           

          w = new Window('dialog')
              e = w.add('edittext')
              g = w.add('group')
                  r1 = g.add('radiobutton {text: "1", value: true}')
                  r2 = g.add('radiobutton {text: "2"}')
          
              r1.onClick = r2.onClick = function(){
                  e.text = this.text
              }
          
          w.show()
          

           

          Not sure what you mean by 'asynchronically', though.

           

          Peter

          • 2. Re: Radiobutton
            pkrk Level 1

            Thanks - That's what I was looking for.

            Glad I asked, using "this" probably wouldn't have occured to me. I'll have to give myself a refresher.

            • 3. Re: Radiobutton
              pkrk Level 1

              I'm assuming the code below won't work because the function isn't called correctly for "this" to apply?

               

              I'm just used to building my functions separately and also wanted to add more code to the function that may make it a bit bulky in the future if I leave it in main().

               

               

              ////////////////

               

              main();

               

               

              function main()

              {

               

               

              try

              {

               

               

              w = new Window('dialog')

                  e = w.add('edittext')

                  g = w.add('group')

                      r1 = g.add('radiobutton {text: "1", value: true}')

                      r2 = g.add('radiobutton {text: "2"}')

               

                  r1.onClick = r2.onClick = wasClicked();

               

              w.show()

               

               

              }catch(e)

              {

                  alert(e);

                  alert(e.line);

                 

                  }

               

               

              }

               

               

              function wasClicked()

              {

                  try{

                     

                      e.text = this.text

                     

                      }

                  catch(e)

                  {

                     

                      alert(e);

                      alert(e.line);

                  }

               

               

              }

              • 4. Re: Radiobutton
                Peter Kahrel Adobe Community Professional & MVP

                Remove the two parentheses after wasClicked (the call, not the function). "That" line would read as follows:

                 

                r1.onClick = r2.onClick = wasClicked

                 

                Don't ask me why and how it works: this is an aspect of JavaScript that I've never gotten my brain around. But I know how to use it, which is fine with me. But if anybody could explain why those parentheses make a difference I'd be happy to learn.

                 

                Peter

                1 person found this helpful
                • 5. Re: Radiobutton
                  MrTIFF Level 3

                  @Peter: One way of looking at it is that, with the parentheses, JavaScript decides that you want to call the function wasClicked right now, when you are assigning it to the property "onClick" of the r1 and r2 objects. wasClicked() returns nothing (null), so null is what gets assigned to r1.onClick and r2.onClick. So, later, when you click a radio button, nothing happens.

                   

                  The longer answer has to do with the fact that, in JavaScript, a function is a first-class Object. In particular, it can have properties, and it can be a property of other objects. See http://msdn.microsoft.com/en-us/magazine/cc163419.aspx, especially the section "JavaScript Functions are First Class". So when you use just the name of the function to assign to the property of an object, the function is not immediately executed. But as soon as you put parentheses at the end, you're telling JavaScript to execute that function right now, which is not what you want here.

                   

                  Is that clear as mud? :-)

                  1 person found this helpful
                  • 6. Re: Radiobutton
                    MrTIFF Level 3

                    Afterthought: one way of seeing this in action is to include the parentheses, put an alert in the try{} part of the wasClicked function, and see what happens. Or set a breakpoint in the try part.

                    • 7. Re: Radiobutton
                      Peter Kahrel Adobe Community Professional & MVP

                      Thanks, Stephen, that makes sense.

                       

                      Peter

                      • 8. Re: Radiobutton
                        pkrk Level 1

                        VERY helpful.

                         

                        Thanks!