TheoryOfNekomata a6bede536f | há 1 ano | |
---|---|---|
cypress | há 1 ano | |
docs | há 1 ano | |
src | há 1 ano | |
.eslintrc | há 1 ano | |
.gitignore | há 1 ano | |
.npmignore | há 1 ano | |
.prettierrc | há 3 anos | |
LICENSE | há 1 ano | |
README.md | há 1 ano | |
cypress.json | há 1 ano | |
package.json | há 1 ano | |
pridepack.json | há 1 ano | |
publish.sh | há 3 anos | |
test-globals.js | há 1 ano | |
tsconfig.eslint.json | há 1 ano | |
tsconfig.json | há 1 ano | |
vitest.config.js | há 1 ano | |
yarn.lock | há 1 ano |
Extract and set form values through the DOM—no frameworks required!
Lightweight. Simple. It Just Works.
The package can be found on:
<select>
and <textarea>
) then bind them
to a form:
<form>
.form=""
attribute then specify the form id
where they will be bound.name=""
attributes to your input elements.<form>
element:
onchange
), use the inputElement.form
attribute.getFormValues()
to retrieve all bound input elements’ values, or setFormValues()
to set them (setting only
some fields’ values is supported).For an example form:
<form id="loginForm" aria-label="Login Form">
<button id="autofill" type="button">
Autofill login form (username: admin)
</button>
<hr />
<fieldset>
<legend>
Login
</legend>
<div>
<input type="text" name="username" placeholder="Username" />
</div>
<div>
<input type="password" name="password" placeholder="Password" />
</div>
<div>
<button type="submit" name="type" value="client">
Log In As Client
</button>
<button type="submit" name="type" value="admin">
Log In As Admin
</button>
</div>
</fieldset>
</form>
<!-- Input elements can be placed outside the form element they are bound to. -->
<label>
<input type="checkbox" name="remember" form="loginForm" />
Remember my login credentials
</label>
Use the library as follows (code is in TypeScript, but can work with JavaScript as well):
import { getFormValues, setFormValues } from '@theoryofnekomata/formxtra';
// This is the only query we need. On libraries like React, we can easily get form elements when we
// attach submit event listeners.
const form: HTMLFormElement = document.getElementById('form');
const processResponse = async (response: Response) => {
const result = await response.json();
alert(`Welcome ${result.user}!`);
};
// Best use case is with event handlers
form.addEventListener('submit', async e => {
const {
currentTarget: thisForm,
submitter,
} = e;
e.preventDefault();
const values = getFormValues(thisForm, { submitter });
// Get the form values and send as request to some API
const response = await fetch(
'https://example.com/api/log-in',
{
method: 'POST',
body: JSON.stringify(values),
headers: {
'Content-Type': 'application/json',
},
},
);
if (response.ok) {
processResponse(response);
}
});
// You can use fomrxtra directly with elements as long as they are bound to a form.
const autofillButton = document.getElementById('autofill');
autofillButton.addEventListener('click', e => {
setFormValues(e.currentTarget.form, { username: 'admin', remember: true });
});
There are utility functions exported alongside getFormValues()
and setFormValues()
. You may want to use namespace
import with this library, i.e. import * as formxtra from '@theoryofnekomata/formxtra'
.
See the documentation folder for more details.
The library has been tested on the static DOM using JSDOM, and the real dynamic DOM using Cypress.