0

In jQuery, is is possible to check if var x = [] changes, and then add the contents of the array to a textbox.

So if x contains ["1","2","3"]. My textbox textbox1 value will be "1","2","3"

Alex Wayne
  • 178,991
  • 47
  • 309
  • 337
JustAnotherDeveloper
  • 3,167
  • 11
  • 37
  • 68
  • 1
    This previous post may help... [http://stackoverflow.com/questions/1029241/javascript-object-watch-for-all-browsers](http://stackoverflow.com/questions/1029241/javascript-object-watch-for-all-browsers). – jabclab Jan 12 '12 at 09:45
  • If you'd use a function to change the value of x, you don't need a watcher (if it's possible at all). – Jeffrey Jan 12 '12 at 09:47

4 Answers4

3

You could do something like this to avoid using a watcher:

var Data = (function () {
    var _x = [],
        $textbox = $("#myTextbox");
    return {
        getX: function () {
            return x;
        },
        setX: function (x) {
            _x = x;
            // Setting 'x' triggers an update
            $textbox.val(x.join(","));
        }
    }
}();

And then set the value of x using Data.setX(x) and get it using Data.getX().

jabclab
  • 14,786
  • 5
  • 54
  • 51
0
var myString = myArray.join(',');
$('#textbox1').val(myString);
Teun Pronk
  • 1,367
  • 12
  • 24
0

Don't know about watching a variable for a change, unless you create an object that has an "onChange" event, but to show the values in the textbox use this...

$("#textboxID").val(x.join(","));

The easiest thing would be to have an update function with the above code and just call that everywhere you change the value of x.

Reinstate Monica Cellio
  • 25,975
  • 6
  • 51
  • 67
-1
if ($.inArray(x, [1, 2, 3]) != -1) {
    $('#textbox1').val(x);
}
Johan
  • 35,120
  • 54
  • 178
  • 293