Reputation: 7003
JSFiddle : http://jsfiddle.net/veksen/7TRgE/1/
If I call the variable value from inside my function, it works. However, it doesn't if I call it outside of the function.
I feel like it has something to do with change, as it is first assigned ok, but won't change.
var diff_res = { norm:0, nm:-40, hell:-100 };
var difficulty = "hell";
$("select").change(function () {
difficulty = $("select option:selected").val();
$(".inside").text(diff_res[difficulty]);
})
.change();
var char_fr = 30;
char_fr += diff_res[difficulty];
$(".outside").text(diff_res[difficulty]);
Upvotes: 0
Views: 375
Reputation: 25081
UPDATE:
By re-executing, I meant something like this:
$(document).ready(function () {
'use strict';
var diff_res = {
norm: 0,
nm: -40,
hell: -100
},
difficulty = "hell",
char_fr = 30,
changeOutside = function changeOutside() {
$(".outside").text(diff_res[difficulty]);
};
$("select").change(function () {
difficulty = $("select option:selected").val();
$(".inside").text(diff_res[difficulty]);
changeOutside();
}).change();
char_fr += diff_res[difficulty];
});
The point was that $(".outside").text(diff_res[difficulty]);
is only being executed once, when the DOM is loaded, which is why the text()
is initially set to -100
. To change the .outside
again, you need to re-run $(".outside").text(diff_res[difficulty]);
ORIGINAL:
Your problem is that:
$(".outside").text(diff_res[difficulty]);
is only executed one time, in the $(document).ready()
function. If you want it to change more often, you'll need to re-execute that in your change
handler.
Upvotes: 1
Reputation: 4974
.inside
have a event which says: "If someone change-me [...]". .outisde
have not events. So:
$(".outside").text(diff_res[difficulty]);
Will execute once only.
See http://jsfiddle.net/7TRgE/2/
Selected option is: "nm" and default value is "-40".
Upvotes: 1