Loading ...
Sorry, an error occurred while loading the content.

RE: [JavaScript] length property of array doesnt work

Expand Messages
  • Donnie Garvich
    ... I saw another response but couldn t read it so I decided to offer something for you to try. The DOM doesn t assign items to an array if there are 0 or 1
    Message 1 of 9 , Dec 3, 2003
    • 0 Attachment
      > hello guys
      > i haveĀ  such a form that when one of the checkbox is checked
      > corresponding textfield enabled or disabled

      I saw another response but couldn't read it so I decided to offer something
      for you to try.

      The DOM doesn't assign items to an array if there are 0 or 1 items listed.
      For example, if you only have one checkbox on a screen an array isn't
      created to hold them in the DOM. Because of this, length isn't available.
      Your alert doesn't work because you misspelled "length."

      Try this:

      function enableDisable() {
      alert(formCourses.checkboxActive.length);

      if (typeof formCourses.checkboxActive != "undefined") {
      if (typeof formCourses.checkboxActive.length != "undefined") {
      for(var i=0; i<=formCourses.checkboxActive.lenght; i++) {
      if (formCourses.checkboxActive[i].checked) {
      formCourses.textfieldGr[i].disabled=false;
      } else { formCourses.textfieldGr[i].disabled = true; }
      }
      } else {
      if (formCourses.checkboxActive.checked) {
      formCourses.textFieldGr.disabled = false;
      } else { formCourses.textfieldGr.disabled = true; }
      }
      }
      }

      This will check to be sure that there are more than one checkbox. It also
      makes sure that at least one exists. ;)

      I didn't verify this code, it may need some syntax tweaking but it will give
      you the right idea.

      :. Donnie
    • iysever
      Thank you very much Jon i correct my mistakes, but i still confused. Who can say me the difference between giving the names checkboxActive and
      Message 2 of 9 , Dec 3, 2003
      • 0 Attachment
        Thank you very much Jon
        i correct my mistakes, but i still confused.

        Who can say me the difference between giving the
        names "checkboxActive" and "checkboxActive[]".
        if there is more than one control using same name these controls
        becomes array. Isnt it?
        and at the second option, checkboxes will become array again. I have
        to use this notation to get the values of checkboxes at the action
        page using PHP.
        But whenever i give the names like checkboxActive[] (without index)
        or checkboxActive[1] (with index), this time my JavaScript function
        doesnt work. That is :(as you advised)

        <input name="checkboxActive[1]" type="checkbox" onClick="enableDisable
        (this.form)" value="">

        function enableDisable(form)
        {
        alert(form.checkboxActive.length);
        for(var i=0; i<=form.checkboxActive.length; i++)
        {
        form.textfieldGrNo[i].disabled=form.checkboxActive[i].checked;
        }

        }

        to use my JavaScript function i change my control names
        to "checkboxActive".

        But this time i cant get the values at the action page. Because PHP
        does not see these controls as array and gives me the only last
        control's value.

        What is this??



        --- In javascript_official@yahoogroups.com, Jon Stephens
        <zontar@m...> wrote:
        > There are two problems
        >
        > 1. your formCourses variable doesn't have have a value.
        >
        > If you want it to refer to the form, you need to assign it to the
        form in some fashion. Here's
        > how I'd do it:
        >
        > <input name="checkboxActive" type="checkbox" value="6"
        onClick="enableDisable(this.form);">
        >
        > function enableDisable(form)
        > {
        > alert(form.checkboxActive.length);
        >
        > for(var i=0; i<=formCourses.checkboxActive.length; i++)
        > // "lenght" was misspelled as "lenght" in your email
        > {
        > form.textfieldGr[i].disabled = form.checkboxActive[i].checked;
        > } // since both properties are booleans there's no need for an
        if/else here...
        > }
        >
        > 2. if this was a copy/paste, then you misspelled "length"
        as "lenght" twice
      • iysever
        Hello friends i dont know why but yahoo gets my mails too late. For this reason i ve posted same message more than once. (Re: length property of array doesnt
        Message 3 of 9 , Dec 4, 2003
        • 0 Attachment
          Hello friends

          i dont know why but yahoo gets my mails too late. For this reason i'
          ve posted same message more than once. (Re: length property of array
          doesnt work)
          I am so sorry everyone because i didnt understand what is going on
          and i thought that my mail didnt arrive

          Regards

          -IYSEVER
        • Jon Stephens
          Ahhhh... Wish you d said you were using PHP at the outset... :^) See: http://www.hiveminds.info/phpBB/viewtopic.php?t=1793 and
          Message 4 of 9 , Dec 4, 2003
          • 0 Attachment
            Ahhhh... Wish you'd said you were using PHP at the outset... :^)

            <shameless_plug>See: http://www.hiveminds.info/phpBB/viewtopic.php?t=1793
            and http://www.hiveminds.info/phpBB/viewtopic.php?t=1717 for some recent discussion of these
            issues on the forum where I hang out (*free registration required to post
            messages).</shameless_plug>

            Right... If you give multiple form controls the same name, yes, that's correct, JS references
            them as an array. Most serverside languages/environments do this also (ASP and Perl that I know
            about for sure)... but not PHP, alas.

            To get around this problem, use the brackets as required by PHP, but don't use the shortcut
            references in JS, use the fully qualified object references instead:

            <input name="checkboxActive[]" type="checkbox" onClick="enableDisable(this.form)" value="">
            <input name="checkboxActive[]" type="checkbox" onClick="enableDisable(this.form)" value="">
            <input name="checkboxActive[]" type="checkbox" onClick="enableDisable(this.form)" value="">
            <input name="checkboxActive[]" type="checkbox" onClick="enableDisable(this.form)" value="">
            <!-- ETC. -->

            function enableDisable(form)
            {
            var checkboxes = form.elements[ "checkboxActive[]" ]; // <<-- !!!
            alert(checkboxes.length);
            for(var i=0; i<=checkboxes.length; i++)
            {
            form.textfieldGrNo[i].disabled=checkboxes[i].checked;
            }
            }

            ... Note: document.myForm.myElement is really just a shortcut for
            document.forms["myForm"].elements["myElements"] ...

            In this case, you're forced to use a name attribute that contains characters that are legal for
            a HTML name attribute, but not legal for a JS identifier... so by using the elements array, you
            get around that issue, since you can use any string as an index for an associative array in JS.

            HTH

            j.


            > Date: Wed, 03 Dec 2003 14:53:30 -0000
            > From: "iysever" <iysever@...>
            > Subject: Re: length property of array doesnt work
            >
            > Thank you very much Jon
            > i correct my mistakes, but i still confused.
            >
            > Who can say me the difference between giving the
            > names "checkboxActive" and "checkboxActive[]".
            > if there is more than one control using same name these controls
            > becomes array. Isnt it?
            > and at the second option, checkboxes will become array again. I have
            > to use this notation to get the values of checkboxes at the action
            > page using PHP.
            > But whenever i give the names checkboxActive[] this time my
            > JavaScript function doesnt work. That is :
            > (as you advised)
            >
            > <input name="checkboxActive[1]" type="checkbox" onClick="enableDisable
            > (this.form)" value="">
            >
            > function enableDisable(form)
            > {
            > alert(form.checkboxActive.length);
            > for(var i=0; i<=form.checkboxActive.length; i++)
            > {
            > form.textfieldGrNo[i].disabled=form.checkboxActive[i].checked;
            > }
            >
            > }
            >
            > to use my JavaScript function i change my control names
            > to "checkboxActive". But this time i cant get the values at the
            > action page. Because PHP does not see these controls as array and
            > gives me the only last control's value.
            >
            > What is this??
          Your message has been successfully submitted and would be delivered to recipients shortly.