Jump to content

function not defined


rick.emmet

Recommended Posts

Hello everyone,

I have a problem that is stumping me. I wrote some JS code that worked fine on two development machines and then work perfectly on the production server. I made a single change to my JS library and tested it, one of my JS function (a show / hide) stopped working. I reverted back to the original code and restested it. The showHide function is still not working. Firebug provides an error message that says "showHide( ); is not defined."

 

I looked at the code and can't see anything wrong with it, so I went online to JSHint and validated the code. It validates. Here's the code in my JavaScript Library:

(function($) {
        // This allows the jQuery object to be used w/o interferance

        // Always nice to use strict mode
        "use strict";

 function ShowHide() {
            var head1 = document.getElementById("head1");
            var showform = document.form1.head1.checked;
            head1.style.visibility = (showform) ? "visible" : "hidden";
        }

})(jQuery);

And here is the JS code embedded in XHTML:

<form name="form1" >
<input type="checkbox" name="head1" onclick="ShowHide();" />Edit Content</form>

Here's what JSHint says of the code:

function ShowHide() {
'ShowHide' is defined but never used.

I don't see any syntax error (and neither does netBeans) and if JSHint says the code is valid, then I have no idea what is wrong. Anyone have a clue? Thanks very much for you help!

Cheers,

Rick

 

 

Link to comment
Share on other sites

Your showHide function is captured within a closure - variables, functions, objects and basically everything defined within a closure is unique to the closure.

 

You don't need jQuery for that function, so define it before you start the closure.

 

 

function showHide() {
var head1 = document.getElementById("head1");
    var showform = document.form1.head1.checked;
    head1.style.visibility = (showform) ? "visible" : "hidden";
}
(function($){
 
...
 
 
})(jQuery);
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.