0

I'm trying to work out how best to handle an input form that is for order processing that basically has a number of identical lines on it for ordering items of clothing.

The controls are all drop down selectors (4 different ones to a line).

At the moment I've just give each one a unique ID and am working through them one by one, but that strikes me as very inefficient.

Is there some way I can use a loop in the html (I don't think this can be done) or some other way to use an array of controls and just iterate over them on the form submit?

1 Answer 1

1

Meteor projects usually include jQuery. jQuery is pretty useful for grabbing groups of elements and running them through a function so that you don't have to repeat yourself as much.

In addition to unique ids, you can add the same class to all or a group of the form elements, and then in the onSubmit callback grab them all with a class selector in jQuery and send them all to a function that combines them into a useful object, e.g.

// collect the form inputs in class salesform into an object

var formResult = {};
$(".salesform").each(function(el){ formResult[$(this)[0].id]=$(this).val() });

// form ids and values are now in formResult

There may also be other selectors you can use instead of tagging the elements you want with a class.

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

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.