How to get Variable value not Variable name.

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

    How to get Variable value not Variable name.

    I am currently working on an ASP page where I create a lot of
    different check boxes. I have some checkboxes that are Windows
    platforms and some that are solaris platforms. I want a control
    checkbox over the Windows ones to turn them all on or all off, and the
    same with the solaris.

    One of the control check boxes looks something like this:

    <input type="checkbox" name="objRS("Pr oductId") & "_solaris"
    value=""0"" onClick=""check All(this.form,n ame);"">

    This gives me a check box with a name that looks like:

    194_solaris

    194 is the productID for a product at my company, and solaris is just
    to tell if its windows or solaris. Now I know this is the name of
    that checkbox this is NOT my problem.

    Now when I pass the onClick=""check All(this.form,n ame) part I go to:

    <SCRIPT LANGUAGE="JavaS cript">
    function checkAll(form, CheckBoxName)
    var myVar = CheckBoxName
    if (form.CheckBoxN ame.checked == true){ <-------- Problem Line
    for (i=0; i<form.elements .length; i++)
    {
    if (form.elements[i].id == myVar)
    form.elements[i].checked = true;
    }
    }else {
    for (i=0; i<form.elements .length; i++)
    {
    if (form.elements[i].id == myVar)
    form.elements[i].checked = false;
    }
    </script>

    Now when I call this I get an error thats says:

    "CheckBoxName.c hecked is null or not declared"

    Well I know good and well CheckBoxName.ch ecked is not declared, but
    the VALUE of CheckBoxName is a valid name. I want the content of the
    variable named CheckBoxName not the name. How can I do this?
  • Dan Brussee

    #2
    Re: How to get Variable value not Variable name.

    In article <189dfa06.03072 41226.78391e96@ posting.google. com>,
    landerud.pete@s tudents.uwlax.e du says...[color=blue]
    > I am currently working on an ASP page where I create a lot of
    > different check boxes. I have some checkboxes that are Windows
    > platforms and some that are solaris platforms. I want a control
    > checkbox over the Windows ones to turn them all on or all off, and the
    > same with the solaris.
    >
    > One of the control check boxes looks something like this:
    >
    > <input type="checkbox" name="objRS("Pr oductId") & "_solaris"
    > value=""0"" onClick=""check All(this.form,n ame);"">
    >
    > This gives me a check box with a name that looks like:
    >
    > 194_solaris
    >
    > 194 is the productID for a product at my company, and solaris is just
    > to tell if its windows or solaris. Now I know this is the name of
    > that checkbox this is NOT my problem.
    >
    > Now when I pass the onClick=""check All(this.form,n ame) part I go to:
    >
    > <SCRIPT LANGUAGE="JavaS cript">
    > function checkAll(form, CheckBoxName)
    > var myVar = CheckBoxName
    > if (form.CheckBoxN ame.checked == true){ <-------- Problem Line
    > for (i=0; i<form.elements .length; i++)
    > {[/color]
    <snippage />[color=blue]
    >
    > "CheckBoxName.c hecked is null or not declared"
    >
    > Well I know good and well CheckBoxName.ch ecked is not declared, but
    > the VALUE of CheckBoxName is a valid name. I want the content of the
    > variable named CheckBoxName not the name. How can I do this?
    >[/color]

    a. I would try not to use the word "form" as a variable name. Too easy
    to get mixed up with the object type. But, if it works - fine.

    if (form [CheckBoxName].checked == true) {

    That should do it for you. Make sure there is a space between the form
    object and the first square bracket.


    --

    Remove NOT from email address to reply. AntiSpam in action.

    Comment

    • YD

      #3
      Re: How to get Variable value not Variable name.

      Pete Mahoney wrote:[color=blue]
      > [...]
      > Now when I pass the onClick=""check All(this.form,n ame) part I go to:[/color]

      You may pass a reference to the checkbox as argument of the JS function :
      onclick="checkA ll(this)"

      and modify the function that way :

      function checkAll(checkb ox){
      var myForm=checkbox .form;
      if(checkbox.che cked) /* no need to compare to true
      (if checkbox is checked your statement is if(true==true), if(true) is enough! */
      //etc.
      }

      HTH
      --
      Y.D.


      Comment

      • Peter Landerud

        #4
        Re: How to get Variable value not Variable name.

        This also does not work. I get an error what says:

        Error, 'checked' is null or not an object

        Check is a part of any checkbox is HTML and I don't know what this
        means. Please help.



        *** Sent via Developersdex http://www.developersdex.com ***
        Don't just participate in USENET...get rewarded for it!

        Comment

        • Richard Cornford

          #5
          Re: How to get Variable value not Variable name.

          "Pete Mahoney" <landerud.pete@ students.uwlax. edu> wrote in message
          news:189dfa06.0 307241226.78391 e96@posting.goo gle.com...
          <snip>[color=blue]
          > <SCRIPT LANGUAGE="JavaS cript">
          > function checkAll(form, CheckBoxName)
          > var myVar = CheckBoxName
          > if (form.CheckBoxN ame.checked == true){
          > for (i=0; i<form.elements .length; i++)
          > {
          > if (form.elements[i].id == myVar)
          > form.elements[i].checked = true;
          > }
          > }else {
          > for (i=0; i<form.elements .length; i++)
          > {
          > if (form.elements[i].id == myVar)
          > form.elements[i].checked = false;
          > }
          > </script>
          >
          > Now when I call this I get an error thats says:
          >
          > "CheckBoxName.c hecked is null or not declared"[/color]
          <snip>

          No it doesn't. The message states that "form.checkNoxN ame is null or not
          an object" ( or local translation). If you are going to state error
          messages it would be a good idea to _read_ the message first.

          See:-

          < URL: http://www.jibbering.com/faq/#FAQ4_39 >

          And read the linked article (sq_brackets.ht ml).

          Richard.


          Comment

          • Peter Landerud

            #6
            Re: How to get Variable value not Variable name.

            This still does not work. I get an error that says:

            Error, 'Checked' is null or not an object

            Checkboxes in HTML have a checked option, why does it think this is an
            object and not a method? Please help.



            *** Sent via Developersdex http://www.developersdex.com ***
            Don't just participate in USENET...get rewarded for it!

            Comment

            • Richard Cornford

              #7
              Re: How to get Variable value not Variable name.

              "Dan Brussee" <dbrussee@NOTbe tterwaycomputin g.com> wrote in message
              news:MPG.198a16 d493495d969897a 9@news-server.nc.rr.co m...
              <snip>[color=blue]
              >... . Make sure there is a space between the form
              >object and the first square bracket.[/color]

              Why?

              Richard.


              Comment

              • Peter Landerud

                #8
                Re: How to get Variable value not Variable name.

                Hi Richard,

                Actually the error message said exactally that. I have tryed the []
                thing and it still gives me that same error. Any idea why this is
                happening?

                I tryed this (myform [CheckName].checked)

                I am 100% sure I have a checkbox with that name, and wonder why I keep
                getting this error.



                *** Sent via Developersdex http://www.developersdex.com ***
                Don't just participate in USENET...get rewarded for it!

                Comment

                • Dan Brussee

                  #9
                  Re: How to get Variable value not Variable name.

                  In article <bfplkr$q75$1$8 300dec7@news.de mon.co.uk>,
                  Richard@litotes .demon.co.uk says...[color=blue]
                  > "Dan Brussee" <dbrussee@NOTbe tterwaycomputin g.com> wrote in message
                  > news:MPG.198a16 d493495d969897a 9@news-server.nc.rr.co m...
                  > <snip>[color=green]
                  > >... . Make sure there is a space between the form
                  > >object and the first square bracket.[/color]
                  >
                  > Why?
                  >[/color]

                  I must admit I do not have documentation to show why I say that, but I
                  do know that I always put the space there and it always works. I have
                  seen code that did not work without it, but I cannot give you the
                  reasons why it did not. I suppose I should find this out some day :)


                  --

                  Remove NOT from email address to reply. AntiSpam in action.

                  Comment

                  • Richard Cornford

                    #10
                    Re: How to get Variable value not Variable name.

                    "Dan Brussee" <dbrussee@NOTbe tterwaycomputin g.com> wrote in message
                    news:MPG.198a2e 932d0054729897a c@news-server.nc.rr.co m...
                    <snip>[color=blue][color=green][color=darkred]
                    >>>... . Make sure there is a space between the form
                    >>>object and the first square bracket.[/color]
                    >>
                    >>Why?[/color]
                    >
                    >I must admit I do not have documentation to show why I
                    >say that, but I do know that I always put the space there
                    >and it always works. I have seen code that did not work
                    >without it, but I cannot give you the reasons why it did
                    >not. I suppose I should find this out some day :)[/color]

                    JavaScript is very tolerant of white space but I think that if you have
                    seen code that did not work without the space then the space or
                    otherwise was not the cause. I never include spaces in square bracket
                    property accessors (I think that it would make the code obscure) and I
                    have never had a problem doing so.

                    Richard.


                    Comment

                    • Richard Cornford

                      #11
                      Re: How to get Variable value not Variable name.

                      "Peter Landerud" <landerud.pete@ students.uwlax. edu> wrote in message
                      news:3f205af8$0 $201$75868355@n ews.frii.net...[color=blue]
                      >Actually the error message said exactally that. I
                      >have tryed the [] thing and it still gives me that
                      >same error. Any idea why this is happening?
                      >
                      > I tryed this (myform [CheckName].checked)
                      >
                      >I am 100% sure I have a checkbox with that name, and
                      >wonder why I keep getting this error.[/color]

                      There is nothing in your script that treats - CheckBoxName.ch ecked - as
                      if it was an object so your browser should not even be attempting to
                      resolve it as an object reference.

                      However, I did notice that you have more opening braces ( - { - ) than
                      closing so it might be that you are using IE to test and suffering an
                      unusual side effect of its excessive error correcting behaviour.
                      Generally Gecko browsers (Mozilla, etc.) and Opera 7 give much better
                      error reports and do not even attempt to second guess erroneous
                      JavaScript.

                      Richard.



                      Comment

                      • Richard Cornford

                        #12
                        Re: How to get Variable value not Variable name.

                        Incidentally, the current HTML specifications forbid the strings in name
                        and ID attributes from starting with a number so your "194_solari s" name
                        attribute is invalid. While most browsers don't seem to care about this
                        it is not realistic to expect invalid HTML to result in a consistent or
                        usable DOM or to expect all browsers to react in the same way to invalid
                        code.

                        Richard.


                        Comment

                        Working...