Update form field by ID?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cmcdermo
    New Member
    • May 2007
    • 27

    #16
    Ah ok i have now changed it to this.

    <script language="javas cript">
    function set_value(id, value) {

    var box = document.getEle mentById('att_W rapColour_${pro duct.code}');

    box.value = value;

    }
    </script>

    And the onclick to this

    onclick="set_va lue(Att_WrapCol our_${product.c ode}, 'taupe');"

    When viewing the source they both look like this.

    <script language="javas cript">

    function set_value(id, value) {

    var box = document.getEle mentById('Att_W rapColour_4060S INGLECANVAS/0433-006');

    box.value = value;

    }

    </script>


    onclick="set_va lue(Att_WrapCol our_4060SINGLEC ANVAS/0433-006, 'taupe');"

    When i click on the image i get

    Att_WrapColour_ 4060SINGLECANVA S is undefined, it seems to be missing the value after the slash. /0433-006

    ??

    Comment

    • gits
      Recognized Expert Moderator Expert
      • May 2007
      • 5390

      #17
      Originally posted by cmcdermo
      Ah ok i have now changed it to this.
      ...
      onclick="set_va lue(Att_WrapCol our_4060SINGLEC ANVAS/0433-006, 'taupe');"
      ...
      ok ... you have to pass the id as a primitive string-value that means put it in single quotes in the above statement. and use id in the function until you pass it and the function receives it (leave it as it was first - the one that i provided ;) )... ok? it should work ;)

      kind regards ...

      ps: the error says that you pass an undefined value to the function ... this is due to the fact that JavaScript interpretes your unquoted id as a variable to pass ...

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #18
        Originally posted by cmcdermo
        ...
        When I click a colour i don't get an error, how do i test if the hidden field has been updated?
        ...
        you may simply change type from 'hidden' to 'text' ... and you will see the set value within the textbox. the input-elements have the same behaviour and the 'hidden' is only an 'undisplayed' textbox ... you may also ask the field for its value after updating it:

        [CODE=javascript]
        // use a onclick or whatever you want to call a function like that:
        function get_value(id) {
        var hidden_field = document.getEle mentById(id);
        alert(hidden_fi eld.value);
        }
        [/CODE]

        you have to pass the correct id (your WrapColour-id) as string and you should get a notify-box with the value of your hidden field ...

        kind regards ...

        Comment

        Working...