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"
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"
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()
.
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.
if ($.inArray(x, [1, 2, 3]) != -1) {
$('#textbox1').val(x);
}