0

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.

4
  • Any particular reason you're not using HTML5 form validation? Commented Jul 15, 2013 at 14:45
  • @MattBall: what about old IE? Commented Jul 15, 2013 at 14:47
  • @Qantas94Heavy (1) I find it very hard to care about any version of IE and (2) shims can fix that. Commented Jul 15, 2013 at 14:54
  • Because I don't want the form to get submitted if it's not validated Commented Jul 15, 2013 at 14:54

1 Answer 1

1

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.

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

2 Comments

I gave the inputs a class called this_form and tried to fetch the values by executing this: var x = document.getElementsByClassName('this_form').value;. It's not returning anything.
the method returns an array you have to iterate over - the value attribute will only be defined on the individual array elements (ie. the individual dom nodes).

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.