# form-control

> A cross-browser helper function to retrieve a form control by name.

Latest version **1.0.0** (published 2015-11-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install form-control
pnpm add form-control
yarn add form-control
bun add form-control
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2015-11-14 |
| First published | 2015-11-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Dominic Barnes |
| Maintainers | dominicbarnes |
| Keywords | browser, control, form, html |

## Links

- npm: https://www.npmjs.com/package/form-control
- Repository: https://github.com/dominicbarnes/form-control
- Homepage: https://github.com/dominicbarnes/form-control#readme
- Issues: https://github.com/dominicbarnes/form-control/issues
- npm.io page: https://npm.io/package/form-control

## Dependencies (1)

- [to-array](https://npm.io/package/to-array.md) ^0.1.4

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.0.0 (latest) — 2015-11-14

## README

# form-control

[![npm version](https://img.shields.io/npm/v/form-control.svg)](https://www.npmjs.com/package/form-control)
[![npm dependencies](https://img.shields.io/david/dominicbarnes/form-control.svg)](https://david-dm.org/dominicbarnes/form-control)
[![npm dev dependencies](https://img.shields.io/david/dev/dominicbarnes/form-control.svg)](https://david-dm.org/dominicbarnes/form-control#info=devDependencies)
[![build status](https://img.shields.io/travis/dominicbarnes/form-control.svg)](https://travis-ci.org/dominicbarnes/form-control)

A cross-browser helper function to retrieve a form control by name.

## Usage

```html
<form id="my-form">
    <fieldset name="user">
        <legend>User Information</legend>

        <input type="text" name="username">
        <input type="password" name="password">
    </fieldset>

    <input type="checkbox" name="accept">
</form>
```

```js
var element = require('form-element');
var form = document.querySelector('#my-form');

element(form, 'username');
// => <input type="text" name="username">

element(form, 'accept');
// => <input type="checkbox" name="accept">

element(form, 'does-not-exist');
// => null ... this element does not exist at all


// restrict search to a specific fieldset

var fieldset = element(form, 'user');

element(fieldset, 'accept');
// => null ... this element is not part of the <fieldset>
```

## API

### element(root, name)

Use this API to find a form control via it's `name` in the given `root` element.

When searching a `<form>` or a `<fieldset>` as the `root`, this is a wrapper for
[`root.elements.namedItem()`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormControlsCollection).

Otherwise, this will use `root.querySelectorAll()` to find elements with the corresponding `name`.
This method is primarilly intended as a fallback, you should strongly consider using a `<form>` or `<fieldset>`
as a first-resort.

If *no* element(s) are found with the given `name`, this function will return `null`.

**PROTIP:** just about any element with a `name` attribute can be found using this mechanism, so give
your `<fieldset>`s names, and then use the API to search just within that specific `<fieldset>`.

---
_Source: https://npm.io/package/form-control · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
