cardboardtheory
cardboardtheory

Reputation: 117

Global variable is not changing on button press (scope issue)

I have a declared variable and jQuery code telling a button to store a form's value as the value of that variable and execute a function.

var verb = "";
$( "#submit" ).click(function() {
   verb = $('#enter').val();
   teConjugate();
});

In the teConjugate() function, I have placed a lot of functions and variables using the "verb" variable that I want to use outside the teConjugate() function. However, when I declare these functions and variables globally, they use the globally declared "verb" (which is an empty string). I've done research and I know that it has something to do with the rendering of the page (as console.log does not raise the same issues).

The only thing I've thought of is putting said functions and variables in to a initialize function which executes on button click, to reduce the hassle of copy pasting everything. However, this did not work either.

Is there any solution? Any help would be appreciated.

JSbin with the dilemma. As one can see, there are a lot of variables and functions that would be more useful outside the function.

Upvotes: 2

Views: 73

Answers (1)

user3559349
user3559349

Reputation:

There is nothing wrong with what you have shown here (see this fiddle). I suspect it is something to do with your teConjugate(); function.

Upvotes: 1

Related Questions