Home:ALL Converter>Method inside another function doesn't work properly

Method inside another function doesn't work properly

Ask Time:2015-01-31T02:26:00         Author:Jake Murphy

Json Formatter

I've created a function(new Soldier) in a .js that creates an object with Class="unit" that when clicked, it will provide some information. When I put the function outside of all other functions(but still inside $(document).ready(function() {}), it works perfectly. But when I put it inside another function(that contains an if statement), it creates the object(or at least the image), but the object doesn't do anything when clicked.

The one that works is simply:

Unit[selectedindex] = new Soldier(selectedindex);

But when set like this:

$('#startingSoldier').click(function() {
    Unit[selectedindex] = new Soldier(selectedindex);
}
});

The object it creates doesn't work when clicked. I have a $('.unit').click(function(){}.

Let me know if you need additional information on the click function or the Soldier method. Thank you for your help.

Author:Jake Murphy,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/28242324/method-inside-another-function-doesnt-work-properly
yy