ottz0
ottz0

Reputation: 2605

Getting values from dynamic select boxes

I have a dynamic select box(s) that generates a unique id with a prefix childs-age.

So I have childs-age-0, childs-age-1, childs-age-2 and so on depending on how many have been selected.

I have used {{'child-age-'+$index}} to generate this.

I need to get the values from the form and put them into an array.

childsAges=[2,5,9]

This is what I have so far

for(var i = 0; i < 4; ++i)
        {
        var childAges = document.getElementById("child-age-" + i);
            angular.forEach(childAges, function(value){
                console.log(childAges.value)
            });
        }

Upvotes: 1

Views: 49

Answers (1)

dfsq
dfsq

Reputation: 193261

This is what I have so far ...

Remove all this DOM reading code with document.getElementById. This is not Angular way.

You need to use ngModel directives and bind each of new selectbox to the dynamic model. For example:

<select ng-model="childAge[$index]">
    <!-- ngOptions or static options ... -->
</select>

Then access ages in controller as:

$scope.childAges

Upvotes: 1

Related Questions