SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast appy's Avatar
    Join Date
    Feb 2002
    Location
    UK
    Posts
    99
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Allowed var names in for use with forms

    Hi All,

    I am trying to adapt a script for use validating a drop-down menu in a form.

    The script works fine in my lttle demo. In the head of the page I've got...

    Code:
    <script type="text/javascript">
    function checkList() {
     var testForm = document.testForm;
     if (testForm.demoSelect[testForm.demoSelect.selectedIndex].value == "") {
      alert("Please make a valid selection from the list.");
      return false;
     }
    }
    </script>
    And then in the body...

    HTML Code:
    <form action="demo.php" method="post" name="testForm" id="testForm" onSubmit="return checkList();"> 
     <p>
      demo select menu
      <select name="demoSelect" id="demoSelect">
       <option value="" selected>Please Select</option>
       <option value="1">Attrib1</option>
       <option value="2">Attrib2</option>
       <option value="3">Attrib3</option>
      </select>
     </p>
     <p>Submit
      <input type="submit" name="Submit" value="Submit">
     </p>
    </form>
    No problem, works fine. The problem is that I am trying to apply this code to a PHP page in osCommerce where the name of the drop-down menu (i.e. demoSelect) is generated dynamically. It is only for use in one particular situation where I know the name of the drop-down menu is generated as id[1] but it wont work when I replace the demoSelect for id[1].

    I'm only really beginner with JS but I guess it's because JavaScript identifies id[1] as an array? Is this right and any ideas how I can correctly identify my drop-down to JS?

    Thanks,

    Pete

  2. #2
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In Javascript there is two notations for accessing object properties

    object.property
    object["property"]

    The latter form is useful when 'property' is not a valid javascript identifier.

  3. #3
    SitePoint Enthusiast appy's Avatar
    Join Date
    Feb 2002
    Location
    UK
    Posts
    99
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks stereofrog,

    You're a star!

    I changed the function to...

    Code:
     
    <script type="text/javascript">
    function checkList() {
     var cart_quantity = document.cart_quantity;
     if (cart_quantity["id[1]"][cart_quantity["id[1]"].selectedIndex].value == "") {
      alert("Please make a valid selection from the list.");
      return false;
     }
    }
    </script>
    ...and it works

    Thanks again.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •