Suzanne Edelman Creoconcept Suzanne Edelman Creoconcept - 1 year ago 179
jQuery Question

jquery - reload function, lose variable value

lets say i have this function:

$("#click").click(function myFunction(){
var id = $(this).attr("id");

Now, obviously, after the click event, id == "click", but when the function reloads, id == undefined.
Is there a way that i can reload the function without redefining the variable?

Answer Source

You're recursively calling a closure function. Here's a working copy where you can use a callback function to access the variable:

$("#click").click(function() {
   var id = $(this).attr("id");
   console.log("click id: " + id);

function myFunction(id) {
   console.log("id: " + id);

JSFiddle Example

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download