ObzidO
ObzidO

Reputation: 11

Meteor Form getting input value with dynamic name

Taking this form from meteor tutorial as example

<form>
        <input type="text" name="playerName">
        <input type="submit" value="Add Player">
</form>

I am able to use event.target.playerName.value to access the value of the input.
However if my input name is generated dynamically, how do I access it?

eg. I have playerName1, playerName2, playerName3 and I want to use a for loop to access them.

    for (i = 1; i <= 3; i++) {
        console.log(event.target.playerName+i.value)
    }

Upvotes: 0

Views: 40

Answers (1)

Mikkel
Mikkel

Reputation: 7777

You can refer to object variables using the array notation as well, for example

for (i = 1; i <= 3; i++) {
    console.log(event.target[playerName+i].value)
}

Upvotes: 1

Related Questions