Daniel Tovesson
Daniel Tovesson

Reputation: 2590

Validate inputs with a certain name using JavaScript

I want to get the inputs inside my form that has a name starting with this_form_. I have a loop creating my inputs so I don't know how many they are. And I have other inputs in the form that's not supposed to be validated.

This is how I create my forms:

<input type="text" name="this_form_<?php echo $i; ?>" value="" />

As I can delete specific inputs the names that I want to fetch can be this_form_100, this_form_101, this_form_102 and this_form_110

I can fetch the input value manually by doing the following:

document.forms['options-form']['this_form_100'].value

I just need a dynamic way to do this.

Upvotes: 0

Views: 127

Answers (1)

collapsar
collapsar

Reputation: 17238

complement your input tags with a css class. thereafter you can select the relevant set using getElementsByClassName ( docs cf. here ).

you might wish to employ jQuery, a free and high quality js framework offering neat methods to handle cases like yours while abstracting away most of the user agent peculiarities.

Upvotes: 1

Related Questions