9 Replies Latest reply on Jan 18, 2011 11:16 AM by darka2011

    script auto selects when i dont want it to

    darka2011

      me again sry so this script

       


      if (this.getField("race").value = "Human"){
      if (this.getField("gender").value = "Male"){
        var h_male = new Array ();
      h_male[0] = "bob";
      h_male[1] = "mathew";
      h_male[2] = "andrew";
      h_male[3] = "stephen";
      h_male[4] = "ben";


      var hm = math.floor(5*Math.random())

      this.getField("character name").value = h_male[hm];}

       

      else (this.getField("race").value = "Human")
      (this.getField("gender").value = "Female");{
        var h_female = new Array ();
      h_female[0] = "michelle";
      h_female[1] = "sarah";
      h_female[2] = "skye";
      h_female[3] = "kayla";
      h_female[4] = "louise";

      var hf = Math.floor(5*Math.random())

      this.getField("character name").value = h_female[hf];}

      }

       

      now it sort of works the problem im having is that when i hit the random button it auto puts gender to male  and random selects from h_female not the h_male

       

      i might be wrong but i think its the (this.getField("gender").value = "Male") is writing to the field gender insted of reading it? if so how do i go about fixing it

       

      Message was edited by: darka2011

        • 1. Re: script auto selects when i dont want it to
          Bernd Alheit Adobe Community Professional & MVP

          To compare values use '==' not '='.

          if (this.getField("race").value == "Human"){

          • 2. Re: script auto selects when i dont want it to
            darka2011 Level 1

            that sorted out the problem with it auto selecting male thanks but it still only out puts from the h_female array when male is selected

            • 3. Re: script auto selects when i dont want it to
              Bernd Alheit Adobe Community Professional & MVP

              Can you explain the following construct?

              else (this.getField("race").value = "Human")
              (this.getField("gender").value = "Female");{

              • 4. Re: script auto selects when i dont want it to
                gkaiseril MVP & Adobe Community Professional

                Are you getting any errors in the JavaScript console?

                 

                I have a message about a missing code block terminator ,"}".

                 

                Once that is fixed, the  next message is about the reference to 'math' is not defined.

                 

                Using the nested if:

                 

                // array for random names
                var aNames = new Array();
                // clear the field
                event.value = '';
                // test for race
                if (this.getField("race").value == "Human"){
                   // human chose
                   // test for human gender
                   if (this.getField("gender").value == "Male"){
                      // human male gender
                      // populate male names
                      aNames = ["bob", "mathew", "andrew", "stephen", "ben"];
                      event.value = aNames[Math.floor(aNames.length * Math.random())];
                   }else if (this.getField("gender").value == "Female"){
                      // human female gender
                      // populate frmale names
                      aNames = ["michelle", "sarah", "skye", "kayla", "louise", 'Zelda'];
                      event.value = aNames[Math.floor(aNames.length * Math.random())];
                   } else {
                      // human not male or female
                      event.value = "";
                   // end human gender options
                } } else {
                   event.value = '';
                }  // end race options

                 

                 

                Using the switch statement:

                 

                // array for random names
                var aNames = new Array();

                if (this.getField("race").value != " " & this.getField("gender").value != " ") {
                switch(1) {

                case (this.getField("race").value == "Human" & this.getField("gender").value == "Male"):
                   // human male gender
                   // populate male names
                      aNames = ["bob", "mathew", "andrew", "stephen", "ben"];
                      event.value = aNames[Math.floor(aNames.length * Math.random())];
                break;

                case (this.getField("race").value == "Human" & this.getField("gender").value == "Female"):
                   // human female gender
                  // populate frmale names
                      aNames = ["michelle", "sarah", "skye", "kayla", "louise", 'zelda'];
                      event.value = aNames[Math.floor(aNames.length * Math.random())];
                break;

                default:
                // for all non-matched items
                app.alert('No match');
                break;
                } // end switch selections
                } else {
                event.value = '';
                } // end if race and gender not empty

                • 5. Re: script auto selects when i dont want it to
                  darka2011 Level 1

                  hi sry yer i fixed the } and math is not defined just abefore i posted but still had it in clipboard like it

                  im going to give these a try now and will the resalt be out in to the text field character name as i carnt see it in it if not where would i put it?

                  • 6. Re: script auto selects when i dont want it to
                    gkaiseril MVP & Adobe Community Professional

                    The scripts are for the 'custom calculation' script for the name field (event referees the the action for the field in the field where the event exist).

                    1 person found this helpful
                    • 7. Re: script auto selects when i dont want it to
                      darka2011 Level 1

                      thank you so much that works great just one quick question again sry if i wanted to change font out put for each one is that pos

                      • 8. Re: script auto selects when i dont want it to
                        gkaiseril MVP & Adobe Community Professional

                        Then you will need to use the 'Rich Text' option and write the necessary JavaScript to format the characters. Look at the 'span' object in the Acrobat JS API Reference.

                        1 person found this helpful
                        • 9. Re: script auto selects when i dont want it to
                          darka2011 Level 1

                          thank you so much for the help i hope to be able to repay the favor one day