7

I am using JavaScript to extract values from some <input> elements in my page.

Do I need to wrap all my <input> elements in <form> elements?

What will happen if I do not?

Does it give access to some better features?

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
timbram
  • 1,797
  • 5
  • 28
  • 49

1 Answers1

14

No you don't.

The form is used to send data to the server, like some kind of search mask to query a database.

You can put all your input tags in one form tag.

There are some advantages using a form:

  1. Some browsers save the data and you don't have to reenter it
  2. You can easily send it by pushing enter, not just by clicking on a button

Rule of thumb: If you have some input elements that logically belong together, then put them in a form. To process the data you can either use javascript (onsubmit) or you can send it directly to the server.

maraca
  • 8,468
  • 3
  • 23
  • 45
  • So, is that like a way to send data to the server without using JavaScript? Is that kind of why the form elements are there? – timbram Apr 19 '15 at 22:59
  • 1
    @timbram: Yes, sending a form using a submit button is the usual way of sending form data to the server, and doesn't require any JavaScript. – Guffa Apr 19 '15 at 23:01
  • 2
    How the forms are typically used changed over time. You could use no form and then send the data on a button click event or you can use a form and specify an action and a submit button, then the data is sent by pushing on the button or enter in an input, unless you intervene and return false onsubmit – maraca Apr 19 '15 at 23:02
  • 1
    @timbram added a rule of thumb, maybe that helps you – maraca Apr 19 '15 at 23:13