mIRU
mIRU

Reputation: 617

get global variable , from function `change`

function getSelectData(id) {
var value='';

    // set value to be the current selected value
value = jQuery(id+" option:selected").val();

// change value whenever the box changes
jQuery(id).change(function () {
    value = jQuery(id+" option:selected").val();
});

console.log(value);
return value;

}

Thanks to @Hogan , he help me but still i can not get wanted result , how i can get variable value , outside function change , here is this example http://jsfiddle.net/ZvuMh/ , and every time when i'm changing select box , value is a . Thank You for your spending time

Upvotes: 0

Views: 810

Answers (2)

Hogan
Hogan

Reputation: 70523

Here, use this as a template to solve your problem:

http://jsfiddle.net/ZvuMh/3/

Upvotes: 0

rob
rob

Reputation: 10119

You can't unless you move it to be a global variable. As it is, the line "console.log(value)" will only print the value before the function change() is called. (change() gets called when something changes....but that is long after getSelectData() has returned).

Upvotes: 1

Related Questions