Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Completely exclude checkbox element from form?

Tags:

html

How can I completely exclude an input (specifically, a checkbox) from a form? Specifically, how to keep it from resetting when the form is reset. There are numerous answers for how to prevent it from being submitted with a form, however none seem to address the issue of form resets.

My problem is that I need to place a checkbox on my page for user control of an option, but it needs to be located in the middle of a bunch of form inputs. Since it is only a user option selection box, and not part of the form, however, I need it to be excluded from all form operations, such as resetting the form or submitting the form.

like image 342
ibrewster Avatar asked Dec 20 '25 19:12

ibrewster


1 Answers

if you are working with modern browsers that support html5 you can use form attribute of

as mdn describes:

The form element that the input element is associated with (its form owner). The value of the attribute must be an id of a element in the same document. If this attribute is not specified, this element must be a descendant of a element. This attribute enables you to place elements anywhere within a document, not just as descendants of their form elements. An input can only be associated with one form.

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input

so you can simple set form attribute to a dummy id which did not exist in the document

this can simple exclude the input from its containing form

<!DOCTYPE html>
<html lang="zh">
  <head>
    <meta charset="utf-8">
    <title>html5 template .html</title>

  </head>

  <body>
    <form>
      <input type="checkbox" name="hello"/>aaa
      <input type="checkbox" name="hello"/>bbb
      <input type="checkbox" name="option" form="dummy"/>ccc
      <input type="submit"/>
      <input type="reset"/>
    </form>
  </body>
</html>

in the code above. when you click reset. option is not reset

when you click submit option is not submit(see address after click submit, something like: ?hello=on&hello=on)

like image 51
qiu-deqing Avatar answered Dec 22 '25 10:12

qiu-deqing



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!