Input elements outside the tag form

Asked

Viewed 249 times

3

I wonder if it is considered a bad practice input attributes outside of the tag form.

In w3schools

The brand <form> is used to create an HTML form for user input. The element <form> may contain one or more of the following form elements:

<input>
<textarea>
<button>
<select>
<option>
<optgroup>
<fieldset>
<label>

In MDN

The HTML element <form> represents a section of a document that contains interactive controls that allow the user to submit information to a given web server.

Is it correct to use such attributes without HTTP calls? , this affects something related to HTML code validation?

  • 1

    If you validate the pages in most HTML validators, there will surely be element errors without form, or form without elements. In matters of SEO I believe I have no problem, in matters of programming I think also not. Maybe you would if you wanted to load these values between pages.

2 answers

2


It depends on what you are considering as "HTML code validation". If it is the result of HTML validation tools that can be found over the internet, including W3C, yes, there is a possibility that your file will become invalid. Does it matter? No. Because if you consider as validation the definitions and specifications of W3C, which is a consortium that defines the web standards, your HTML file will remain valid.

In the documentation itself on the MDN it is possible to check:

Permitted parent elements Any element that accepts phrasing content.

That is, any element that accepts phrasing content may contain elements input, button, select, etc. Note that at no time is it described that you must be inside an element form.

The very specification of the W3C behind that same stretch:

Contexts in which this element can be used: Where phrasing content is expected.

It is considered bad practice to use them outside the form? Not necessarily. The correct question to ask is: does it make sense in my application that I use them like this? If the answer is yes, you can use them without problems. It is even common for developers to mistakenly use other elements for fear of using such "form" elements. A classic example is to use an anchor element, a, to open a modal window; semantically it would be better to use the modal window itself button, even outside of a form or to send information to the server.

  • This passage really went unnoticed, but in relation to the question, 'make sense', practically everything on front makes, you have the freedom to define for example ul s and li s to replace a select, does it make sense? does, many frameworks use it, is that correct? I think not, it is a hack, many people do not know that there is no select, but anyway, thanks for the reply.

  • 1

    It doesn’t always work. It can work and it can’t make sense at the same time. There has been a user question here wanting to use the checkbox as radio just because it was square. It works? In a way, yes, when taken care of. It makes sense? None at all.

1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.