0

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]);

2
  • Do you have a "selected" option? Commented Nov 11, 2012 at 2:45
  • yes I do, <option value="hell" selected>Hell</option> (html5) Commented Nov 11, 2012 at 2:46

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Adding a handler around the "outside" code causes me trouble, as it includes a var += value inside of a each handler, causing the outside code to add up every time I change the dropdown
Thanks a lot so far! jsfiddle.net/veksen/7TRgE/4 If you check the JSFiddle, you'll see that test1 and test2 update just fine, however with the variable alone (in test3), it doesn't. My current project uses the variable by itself (with (diff_res[difficulty]) added outside, prior to inserting. Which I could adapt just fine.... however, I'm puzzled on to why it happens?
The value of .test3 doesn't change because the source variable (char_fr) never has its value changed from 30. .test3 will change when the value of char_fr has changed (although the select.change() event will have to fire to initiate the change in .test3). Change it to $(".test3").text(char_fr+=300); to update the value of the variable at the same time you display it.
1

.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".

1 Comment

Well, I can get the bit of code outside to update, but it doesn't behave well at all with my code that uses .each to find data in the html and adds it with a +=, only adding it around the outside selector has no effect (probably from another outside variable?) it works in the jsfiddle, but not on my full project

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.