13 Replies Latest reply on Oct 22, 2009 1:53 PM by CompTerra

    Javascript that affects current text field only

    CompTerra

      I am trying to figure out a script that would only affect the current text field I am in without knowing the field name (this.getField) I want to change the properties of whichever text field I am currently typing into (text color, etc), this script would be placed in a button on the document and when pressed would only affect the text field I am currently in.

       

      Thanks

        • 1. Re: Javascript that affects current text field only
          try67 MVP & Adobe Community Professional

          When you click on a button you're no longer in any text field, and anyway there's no way to do it like that.

          • 2. Re: Javascript that affects current text field only
            try67 MVP & Adobe Community Professional

            I was thinking about it, and maybe there is a way.

            If you add a custom OnEnter script to each text field that writes the field's name (maybe with event.target.name) to a global variable, and then access that global variable from your button, then it might work.

            • 3. Re: Javascript that affects current text field only
              MarkWalsh Level 4

              You could set up a document level variable to keep track of the current field, and for each field, add a script to the blur and focus events to update that variable. Then, in the mouse enter event for your button, set a second variable to the contents of that variable. That way, when you click the button, the active field name will be retained

               

               

              Add this to your document scripts:

               

               

              var currentFieldName // Name of field set when any field is entered

              var activeField // Name of active field set with button mouse over

               

              function setCurrentField (fieldName) {

              currentFieldName = fieldName

              }

               

              function setActiveField () {

              activeField = currentFieldName

              }

               

              function buttonClick() {

              app.alert (activeField)

              }

               

               

              Add this to the Focus event for every field:

              setCurrentField (event.target.name)

               

               

              Add this to the Blur event for every field:

               

              setCurrentField ('')

               

               

               

              Add this to the Mouse Enter event for the button:

               

              setActiveField ()

               

               

              1 person found this helpful
              • 4. Re: Javascript that affects current text field only
                MarkWalsh Level 4

                try67 wrote:

                 

                I was thinking about it, and maybe there is a way.

                If you add a custom OnEnter script to each text field that writes the field's name (maybe with event.target.name) to a global variable, and then access that global variable from your button, then it might work.

                Darn, you beat me to it while I was writing the code. 

                • 5. Re: Javascript that affects current text field only
                  try67 MVP & Adobe Community Professional

                   

                   

                  I still think one variable would suffice, though. You overwrite it each time

                  you enter a new field and when the button is clicked it uses its value and

                  then sets it to "". Wouldn't that work as well?

                  • 6. Re: Javascript that affects current text field only
                    MarkWalsh Level 4

                    By using 2 variables, you can tell whether or not a field is active when you click the button. With just one field, you can only tell the last entered field, but not if it was actually active when the button was clicked (which is why I have them cleared when the field is exited).

                    • 7. Re: Javascript that affects current text field only
                      try67 MVP & Adobe Community Professional

                      I see what you mean. Your script works if the user goes directly from the

                      text field to the button.

                      But if for example the user first clicks somewhere else in the document (on

                      another form field or not), then the value will be deleted and the button

                      won't be able to get it anymore.

                      In my version you can click anywhere you want, even on other form fields,

                      and the button will still be able to recognize what was the last active text

                      field.

                       

                      At the end it depends on what you want to achieve, I guess...

                      1 person found this helpful
                      • 8. Re: Javascript that affects current text field only
                        MarkWalsh Level 4

                        Yes, the simpler version is probably suitable to the task (and may be the desired result; I don't know from the original post if they care if the field is still active or not), but if they are interested in the active field only if it is still active, then the second variable is necessary.

                         

                         

                         

                         

                         

                         

                        ... we could always add a 3rd variable so the user can use both methods in the same document...

                        • 9. Re: Javascript that affects current text field only
                          MarkWalsh Level 4

                          Actually, this modification makes the script much more versatile - still using 2 variables, but don't need the mouseover anymore for the button, and you can decide whether or not you care if the field is active:

                           

                           

                           

                           

                          var currentFieldName // Name of field set when any field is entered

                          var fieldActive // Boolean to determine if field is still active

                           

                          function setCurrentField (fieldName) {

                               if (fieldName != '') {

                                    currentFieldName = fieldName

                               }

                               fieldActive = (fieldName != '')

                          }

                           

                          function buttonClick() {

                               if (fieldActive) {

                                    app.alert (currentFieldName + ' is active.' )

                               } else {

                                    app.alert (currentFieldName + ' is no longer active.')

                               }

                          }

                           

                           

                           

                           

                          Add this to the Focus event for every field:

                           

                          setCurrentField (event.target.name)

                           

                           

                           

                          Add this to the Blur event for every field:

                           

                          setCurrentField ('')

                           

                           

                           

                          Add this to the Click event for the button

                           

                          buttonClick ()

                           

                           

                           

                           

                          • 10. Re: Javascript that affects current text field only
                            gkaiseril MVP & Adobe Community Professional

                            You can use the "event.target.name"  or the 'event.targetName' to obtain the name of the triggering field.

                             

                            Event properties:

                             

                            target -  The target object that triggered the event. In all mouse, focus, blur, calculate, validate, and format events, it is the Field object that triggered the event. In other events, such as page open and close, it is the Doc or this object.

                             

                             

                             

                             

                             

                             

                             

                             

                            targetName - Tries to return the name of the JavaScript being executed. Can be used for debugging purposes to help identify the code causing exceptions to be thrown. Common values of

                            targetName include:

                            The folder-level script file name for App/Init events

                            The document-level script name forDoc/Open events

                            The PDF file name being processed for Batch/Exec events

                            he field name for Field events

                            The menu item name for Menu/Exec events

                            The screen annotation name for Screen events (multimedia events)

                             

                            When an exception is thrown,

                            targetName is reported if there is an identifiable name.

                             

                             

                            • 11. Re: Javascript that affects current text field only
                              MarkWalsh Level 4

                              The OP is trying to get information about the active field via a button click. event.target will be the button in this case (since the button triggered the event, not the text field), not the (previously) active field.

                              • 12. Re: Javascript that affects current text field only
                                MarkWalsh Level 4

                                MarkWalsh wrote:

                                 

                                 

                                Actually, this modification makes the script much more versatile - still using 2 variables, but don't need the mouseover anymore for the button, and you can decide whether or not you care if the field is active:

                                 

                                 

                                 

                                 

                                var currentFieldName // Name of field set when any field is entered

                                var fieldActive // Boolean to determine if field is still active

                                 

                                function setCurrentField (fieldName) {

                                     if (fieldName != '') {

                                          currentFieldName = fieldName

                                     }

                                     fieldActive = (fieldName != '')

                                }

                                 

                                function buttonClick() {

                                     if (fieldActive) {

                                          app.alert (currentFieldName + ' is active.' )

                                     } else {

                                          app.alert (currentFieldName + ' is no longer active.')

                                     }

                                }

                                 

                                 

                                 

                                 

                                Add this to the Focus event for every field:

                                 

                                setCurrentField (event.target.name)

                                 

                                 

                                 

                                 

                                Add this to the Blur event for every field:

                                 

                                setCurrentField ('')

                                 

                                 

                                 

                                 

                                Add this to the Click event for the button

                                 

                                buttonClick ()

                                 

                                 

                                 

                                 

                                 

                                 

                                 

                                 

                                Did a quick test of this, and realized the mouse enter is still necessary (as well as a 3rd variable) to be able to determine if the field was active when the button was pressed and retain the field name. Modifications below:

                                 

                                Change the document script to this:

                                var currentFieldName // Name of field set when any field is entered

                                var fieldActive // Boolean to determine if field is still active

                                var currentFieldActive

                                 

                                 

                                function setCurrentField (fieldName) {

                                     if (fieldName != '') {

                                          currentFieldName = fieldName

                                     }

                                     fieldActive = (fieldName != '')

                                }

                                 

                                function buttonClick() {

                                     if (currentFieldActive) {

                                          app.alert (currentFieldName + ' is active.' )

                                     } else {

                                          app.alert (currentFieldName + ' is no longer active.')

                                     }

                                }

                                 

                                function setCurrentFieldActive() {

                                     currentFieldActive = fieldActive

                                }

                                 

                                 

                                 

                                 

                                 

                                Add this to the Focus event for every field:

                                 

                                setCurrentField (event.target.name)

                                 

                                 

                                 

                                 

                                Add this to the Blur event for every field:

                                 

                                setCurrentField ('')

                                 

                                 

                                 

                                 

                                Add this to the Click event for the button

                                 

                                buttonClick ()

                                 

                                 

                                 

                                 

                                Add this to the Mouse Enter event for the button:

                                 

                                setCurrentFieldActive()

                                 

                                • 13. Re: Javascript that affects current text field only
                                  CompTerra Level 1

                                  Excellent work guys...Thank you very much, the above scripts worked for what I needed, I adjusted a tad for my own but they worked...GREAT JOB.