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"

2

4 Answers 4

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().

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

Comments

0
var myString = myArray.join(',');
$('#textbox1').val(myString);

Comments

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.

Comments

-1
if ($.inArray(x, [1, 2, 3]) != -1) {
    $('#textbox1').val(x);
}

1 Comment

I think that it is not checking for one particular content which is important but to check when the content of x changes...

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.