Error message on a Javascript variable definition

Posted on 2007-10-09
Last Modified: 2010-08-05

Hopefully an easy one. I am using the solution provided in:

and am getting an error when the page loads stating 'thebox' is undefined.

thebox is the variable in the javascript code. The page works fine, but the little Error on Page is nagging me. How do I define the variable and not screw everything else up. I tried adding Var thebox; within the script tages but then it errors on the var checked boxes saying it is null or without a value.

Thanks for a quick response!
Question by:webkiwi1
    LVL 63

    Accepted Solution

    The var thebox is a parameter variable passed to the function toggleBoxes(thebox). But it is passed only if you call the function with the predefined reference: "this"
    Like here:
    <td align="center"><input type="checkbox" name="Chk" onclick="return toggleBoxes(this)" value="<%=C1%>"></td>


    Author Comment

    OK, I am doing that in an include file:
    <td class="style4" align="center"><input type="checkbox" name="Chk" onclick="return toggleBoxes(this)" value="<%=fPin%>"></td>

    How do I get rid of the undefined error?
    LVL 9

    Assisted Solution

    One suggestion is to pass your code through

    It is very strict at times, but it has helped me a LOT at pinning down pesky errors.

    Author Comment

    checked and tried jslint but:

    JavaScript's biggest problem is its dependence on global variables, particularly on implied global variables. If a variable is not explicitly declared (usually with the var statement), then Javascript assumes that the variable was global. This can mask misspelled names and other problems.

    JSLint expects that all variables and functions are declared before they are used or invoked. This allows it to detect implied global variables. It is also good practice because it makes programs easier to read.

    This is the crux of my question, how/where do I declare the variable 'thebox'?
    LVL 63

    Expert Comment

    The variable thebox has NOT to be defined before. It would not be an error but it is not used as var. It is a function PARAMETER that needs to be passed to the function.
    The parameter is passed to function call and it is a reference to an element on the document. If you do not pass a valid reference, then is the parameter thebox not defined.

    And the call parameter needs to be passed in the onclick event handler. Can we see your page live on internet? Or can you post the html source of the page as seen in the browser?


    Author Comment

    Moved on to a different project now and do not have time to work through this one. The page loads and works etc just shows the javascript error!

    Points awarded for the help.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    In my daily work (mainly using, I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
    Article by: DanRollins
    This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (, we'll extend the program by adding a depth-…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now