0

I am trying to add values to a textbox when looping through an array when checking checkboxes but as it is at the moment getting undefined.

Advice perhaps as to why the values are 'undefined'

    var txtBoxValues = [];
    $(document).on("click", "input[name=chkRelatedTopics]", function () {

    var nameAdminUser = $(this).val();
    var txtBox = document.getElementById("txtTraningTopics");
    txtBox.value = '';

    txtBoxValues.push(nameAdminUser);

    for (var i in txtBoxValues) {

        var str = txtBoxValues[i].value;
        txtBox.value += str + '; ';

    }
});
1
  • Do you know how to use firebug or Chrome console? printing the value of txtBoxValues[i] would be enough to spot the issue Commented Sep 2, 2013 at 14:13

2 Answers 2

2

nameAdminUser is already a string, so don't take .value from it.

You could replace

 var str = txtBoxValues[i].value;

with

 var str = txtBoxValues[i];

But instead of using this loop, and assuming you don't want, as I suppose, the last ";", you could also do

txtBox.value = txtBoxValues.join(';');
Sign up to request clarification or add additional context in comments.

Comments

1

nameAdminUser seems to be a String and in your for loop you expect an object. What if you simply do:

for (var i in txtBoxValues) {
    var str = txtBoxValues[i];
    txtBox.value += str + '; ';
}

Comments

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.