Question: I have the following HTML:

```
<input type='checkbox' onchange='checkvalue(this)' def='checked' value='checked' />

```

and the following javascript:

```
$(document).ready(function(){
function checkvalue(){
if($(this).attr("value") != "checked"){
($(this).attr("value") = "checked");
}
else{
   ($(this).attr("value") = "unchecked");
}
}
});

```

Basically whenever a checkbox is checked or unchecked I want to call this javascript function which will see whether or not the default value of the checkbox is checked or unchecked, and then change the value accordingly - if a checkbox had default value of checked and is clicked, the value should change to unchecked and vice versa. However, I get the following error when I click on my checkbox:

```
Uncaught ReferenceError: checkvalue is not defined (index):38
onchange

```

Here is the link to my jsfiddle: <http://jsfiddle.net/LRneA/>

Answer: 
`checkvalue` is not in the global scope. It is local to the `ready` handler, which is an anonymous function.

I suggest not using `onclick="..."`. If you're already using jQuery, bind the event using jQuery also:

```
$("#myCheckBox").on("click", checkvalue); //you will need to give your checkbox
                                          //an id

```

You can do this inside the `onReady` handler.

Another thing I noticed:

```
if($(this).attr("value") != "checked")

```

Isn't going to work. The value of the checkbox has nothing to do with it being checked or not. Use the `:checked` pseudoselector instead along with `.is`:

```
if($(this).is(":checked"))

```
The function/identifier `checkvalue` is *local* to the "on ready" function.

To use `checkvalue` from an inline event attribute, you must make it accessible from the relevant (global) scope. For instance,

```
$(document).ready(function(){
   function checkvalue(){
     // ..
   }
   window.checkvalue = checkvalue;
});

```

However, simply *eliminating* the use of inline events is often preferred. If the element ID is not known, or the event must be applied to many elements, then [Event Delegation](https://learn.jquery.com/events/event-delegation/) (i.e. [`.on`](https://api.jquery.com/on/)) can be used.

```
jQuery(function($){
   function checkvalue() {
     // ..
   }

   // ideally you'd pick a more refined ancestor than "document"
   $(document).on("click", "checkbox", checkvalue);
});

```

And, if you *do* continue with inline events, and you *do* fix the first mentioned issue, then you'll also have to use `apply` to correctly setup the context or `this` will evaluate to the window object in the handler, which will be no fun and lead to other errors.

```
onclick="checkvalue.apply(this)"

```