1

I have a form with about 20 input fields. I capture values of these fields, then do some calculations and output several values.

Is there a preferred/recommended way of capturing form data? Currently I store every form field into a separate variable. I was wondering if storing it to an array would be a better and more effective approach.

I'm quite new to Javascript and programming in general, trying to learn the best practices.

1

4 Answers 4

2

My best practice on this depends on what I have to do with the data. If I do not need to loop through it, or send it to another page/service, then there's nothing wrong with individual scoped-variables.

If I need to loop at all, I commonly use an array or object to loop through.

If I have to pass it to another page/service, I make one object variable to encapsulate the rest of them, so I can "stringify" it to JSON and parse back to an object on the other end.

Just my opinion,

Pete

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

Comments

0

You might consider the third approach - just use the data from the form without storing it anywhere.

First check the correctness, once it is considered correct just use what you have.

2 Comments

In the calculations I'm doing I need to use some of that data in several different formulas so storing them into a variable/array seemed like the right approach to me. But I could be wrong.
I would put the form in variable, then use something like form['value'] in calculations. For the variables that are used more often you could put them in the variable. Putting them in the array wouldn't give you much benefit, form is a sort of array, so no need to add additional arrays.
0

You should always assign the attribute "name=..." to an input element, so you can use something like:

var form = document.forms['form'];
var email = form['email'];
email = do something

if you use javascript... if you use jquery it's simple $('input[name="email"]') = do something

I prefer this way because you can call variables by name, not by number, for example "form[0] that corresponds to input[name="email"]".

2 Comments

This is what I'm currently doing but having 20 - 30 variables seemed a little bit overwhelming to me so I was wondering if there is a better way to work with the form data.
I suggest you to continue on this way, then you can check all them with some functions. If you have 2 or more input that need the same control you haven't to write 20 functions but just 1 for all of them.
0

Use the associative properties of arrays in JavaScript to get the benefits of unique field names and OOP:

var formModel = new Array();

formModel['myField'] = getMyFieldValue(); // make 'myField' index to any object

Then you can do things like:

  • formModel['myField'] // get the value
  • formModel.length; // number of fields added
  • for (entry in formModel) { /* loop thru entries */ }
  • formModel.sort([compareFunction]) // custom sort
  • formModel = []; // clear the model
  • Convert array to JSON
  • Any of these ArrayMDN conveniences.

Just one approach, but arrays in JS are extremely versatile and IMO underused objects in JS.

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.