#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    3
    Rep Power
    0

    Noob JS question


    JS give an Syntax error when I try to process the below JS. Can you tell me why and how to fix this?

    error msg:: SyntaxError: missing ; before statement 2: at line 3


    // Place all pre-population data into a single data structure
    var Cost Method = { II Indirect Cost - District Wide:{ Ratio a1: ".16", Ratio a4: ".04", Ratio a5: ".26", Ratio a6: ".11", Ratio a7: ".43" }, III Indirect Cost - Park Only:{ Ratio a4: ".06", Ratio a5: ".28", Ratio a6: ".13", Ratio a7: ".53" }, IV Indirect Cost - Southside :{ Ratio a4: ".09", Ratio a5: ".63", Ratio a6: ".28" }};
    function SetFieldValues(Cost) {
    // Populate fields with values from the Cost Method Object
    this.getField("Ratio a1").value = Cost Method[Cost].Ratio a1;this.getField("Ratio a4").value = Cost Method[Cost].Ratio a4;this.getField("Ratio a5").value = Cost Method[Cost].Ratio a5;this.getField("Ratio a6").value = Cost Method[Cost].Ratio a6;this.getField("Ratio a7").value = Cost Method[Cost].Ratio a7;
    }
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Location
    Pennsylvania, USA
    Posts
    35
    Rep Power
    1
    Look, you shouldn't have spaces within variable names. It should be more like:

    Code:
    // Place all pre-population data into a single data structure
    var Cost_Method = {
      Indirect_Cost_District_Wide:
        { Ratio_a1: ".16",
          Ratio_a4: ".04",
          Ratio_a5: ".26",
          Ratio_a6: ".11",
          Ratio_a7: ".43"
        },
      Indirect_Cost_Park_Only:
      {
        Ratio_a4: ".06",
        Ratio_a5: ".28",
        Ratio_a6: ".13",
        Ratio_a7: ".53" },
      Indirect_Cost_Southside :
      {
        Ratio_a4: ".09",
        Ratio_a5: ".63",
        Ratio_a6: ".28"
      }
    }; // End var Cost_Method
    I can't verify the logic of the rest of your code, but it does not look right to send one instance of "Cost" and then expect all of the ratio fields to be available, even though they differ for each type of "Cost".
    Like my answer? No? Give us a visit anyway robowhiz.info.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    3
    Rep Power
    0
    I'm doing this for a pre-populated PDF form from a drop down list.
    I have different ratio fields which will be pre populated by different selections I will choose from the drop down field. For example,

    When I choose "II Indirect Cost - District Wide" selection from the "Cost Method" field, I want to populate a set of ratios on 5 different field boxes: ratio a1~ratio a5 per se.
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Hi,

    first of all, you're in the wrong forum. Java and JavaScript have nothing to do with each other except that they share the first letters of their name -- but that's also true for "ham" and "hamster".

    As to your question: Whatever you wanna do, you can't change the syntax rules of JavaScript. Variables names must be single words. Period.

    When it comes to object properties, though, you can in fact include spaces as long as you quote the properties:

    javascript Code:
    var obj = {
    	"Look, I'm actually a valid property name!": 42
    };

    Whether this makes sense in a different question. Most people would probably prefer a proper name and put the string into the value.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  8. #5
  9. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,874
    Rep Power
    6351
    Thread moved to the proper forum.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    3
    Rep Power
    0
    Originally Posted by ManiacDan
    Thread moved to the proper forum.
    SO I got what I wanted after taking some time looking and learning at the javascript I wrote. Thank you all.

    // Place all pre-population data into a single data structure
    var CM = {
    "II Indirect Cost - District Wide":
    { Ratioa1: ".16",
    Ratioa4: ".04",
    Ratioa5: ".26",
    Ratioa6: ".11",
    Ratioa7: ".43"
    },
    "III Indirect Cost - Park Only":
    { Ratioa1: "0",
    Ratioa4: ".06",
    Ratioa5: ".28",
    Ratioa6: ".13",
    Ratioa7: ".53"
    },
    "IV Indirect Cost - Southside":
    { Ratioa1: "0",
    Ratioa4: ".09",
    Ratioa5: ".63",
    Ratioa6: ".28",
    Ratioa7: "0"
    },
    "V Indirect Cost - Other":
    { Ratioa1: "0",
    Ratioa4: "0",
    Ratioa5: "0",
    Ratioa6: "0",
    Ratioa7: "0"
    },
    "I Direct Cost":
    { Ratioa1: "0",
    Ratioa4: "0",
    Ratioa5: "0",
    Ratioa6: "0",
    Ratioa7: "0"
    },
    " ":
    { Ratioa1: "0",
    Ratioa4: "0",
    Ratioa5: "0",
    Ratioa6: "0",
    Ratioa7: "0"
    }

    }; // End var CM

    function SetFieldValues(cCost_Method) {
    // Populate fields with values from the Cost Method Object
    this.getField("Ratio a1").value = CM[cCost_Method].Ratioa1;
    this.getField("Ratio a4").value = CM[cCost_Method].Ratioa4;
    this.getField("Ratio a5").value = CM[cCost_Method].Ratioa5;
    this.getField("Ratio a6").value = CM[cCost_Method].Ratioa6;
    this.getField("Ratio a7").value = CM[cCost_Method].Ratioa7;
    }

IMN logo majestic logo threadwatch logo seochat tools logo