Dynamically dependant listBoxes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • nadia

    Dynamically dependant listBoxes

    I am have used php to create a two sets of arrays of listboxes, each
    of the listbox in the array have a unique ID. One of the list boxes
    are dependant on the other one. I have written the code in javascript,
    well changed existing code. I need to be able to select the element by
    ID rather than name. The code was working before I changed things to
    "getElementByID ". I am not very good at javascript so I am not sure if
    I am on the right track, currently nothing is happening.

    This is the Javascript code:
    function swapName(form, i) {
    var Type = 'lsType' + i;
    var Name = 'lsName' + i;
    Type = document.getEle mentByID(Type). options[document.getEle mentByID(Type). selectedIndex].value;

    // this bit resets the name select list to nothing.
    while (document.getEl ementByID(Name) .options.length > 1)
    document.getEle mentByID(Name). options[0] = null;
    // this bit populates the name select list with the required cities.
    if (Type.length > 0) {
    current_array=N ames[Type];

    for (j=0;j<current_ array.length;j+ +) {
    var optionName = new Option(current_ array[j], current_array[j],
    false);
    document.getEle mentByID(Name). options[document.getEle mentByID(Name). length]
    = optionName;
    }
    }
    }

    This is the bit that calls it:
    <select name="lsType[]" onChange="swapN ame(this.form,' .$i.')"
    id="lsType'.$i. '">

    Any suggestions would be welcome
    Thanks in advance!
  • Richard Cornford

    #2
    Re: Dynamically dependant listBoxes

    "nadia" <nsm38@student. cpit.ac.nz> wrote in message
    news:9caa8908.0 309281927.78d27 85@posting.goog le.com...
    <snip>[color=blue]
    > This is the Javascript code:
    > function swapName(form, i) {
    > var Type = 'lsType' + i;[/color]
    <snip>[color=blue]
    > This is the bit that calls it:
    > <select name="lsType[]" onChange="swapN ame(this.form,' .$i.')"
    > id="lsType'.$i. '">[/color]
    <snip>

    When you build the Type local variable to use as the ID you are not
    including the two single quote characters that you have used in the ID
    attribute string. Those single quotes are illegal characters in HTML
    terms, as is the $ and the [ and ] in the name attribute so even if you
    do include them in when building the Type local variable you should not
    have any expectation of the result working on more than just some (if
    common) HTML browsers.

    From the HTML specification:-
    <quote>
    ID and NAME tokens must begin with a letter ([A-Za-z]) and may be
    followed by any number of letters, digits ([0-9]), hyphens ("-"),
    underscores ("_"), colons (":"), and periods (".").
    </quote>

    Richard.


    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: Dynamically dependant listBoxes

      nsm38@student.c pit.ac.nz (nadia) writes:
      [color=blue]
      > The code was working before I changed things to
      > "getElementByID ".[/color]

      Then it is probably because it is with a lower case "d":
      document.getEle mentById
      ^[color=blue]
      > This is the Javascript code:
      > function swapName(form, i) {
      > var Type = 'lsType' + i;
      > var Name = 'lsName' + i;
      > Type = document.getEle mentByID(Type). options[document.getEle mentByID(Type). selectedIndex].value;[/color]

      I would split this into two lines:
      var selElem = document.getEle mentById(Type);
      var selValue = selElem.options[selElem.selecte dIndex].value;

      (purely for style, I don't want to reuse variables more than necessary).
      [color=blue]
      > <select name="lsType[]" onChange="swapN ame(this.form,' .$i.')"
      > id="lsType'.$i. '">[/color]

      I assume this is part of a PHP string declaration, and the the value
      of $i is concatenated with the rest of the string. Be aware that not
      everybody in this newsgroup knows PHP (I know only a little), and that
      the PHP code used to generate the page really isn't interesting to us.
      What is interesting is the HTML that is generated, since that is where
      the bug is.

      /L
      --
      Lasse Reichstein Nielsen - lrn@hotpop.com
      Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
      'Faith without judgement merely degrades the spirit divine.'

      Comment

      Working...