onchange javascript variable

javascript, jquery, onchange, variables

Solution

(Something seems a bit carelessly planned in your code if you need functionality like that)

The easiest way to add that feature is to create a function for updating your variable, that also calls whatever other function you want to.

Instead of:

var test = 1;
test = 2; // calls a javascript function
test = 3; // calls a javascript function

You do:

var test = 1;

function set_test(newval) {
  test = newval;
  my_callback(); // this is whatever you wanted to call onChange
}

set_test(2);
set_test(3);

Problem

Is there a way to call a JavaScript function if a javascript variable changes values using jQuery? Something to the extend of - ``` var test = 1; test = 2; // calls a javascript function test = 3; // calls a javascript function ``` This way I wouldn't have to add an onchange event to so many different functions.

Original source