Extract and set form values through the DOM—no frameworks required! https://github.com/TheoryOfNekomata/formxtra
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
TheoryOfNekomata 48166877e4 Add support for multiple values há 1 ano
cypress Add support for multiple values há 1 ano
docs Update README há 1 ano
src Add support for multiple values há 1 ano
.eslintrc Switch to pridepack, add setFormValues há 1 ano
.gitignore Update README há 3 anos
.npmignore Update npmignore há 1 ano
.prettierrc Separate prettierrc há 3 anos
LICENSE Switch to pridepack, add setFormValues há 1 ano
README.md Update README há 1 ano
cypress.json Add support for multiple values há 1 ano
package.json Refactor code há 1 ano
pridepack.json Switch to pridepack, add setFormValues há 1 ano
publish.sh Update usage notes, prepare for publishing há 3 anos
test-globals.js Switch to pridepack, add setFormValues há 1 ano
tsconfig.eslint.json Update tsconfig há 1 ano
tsconfig.json Update tsconfig há 1 ano
vitest.config.js Switch to pridepack, add setFormValues há 1 ano
yarn.lock Switch to pridepack, add setFormValues há 1 ano

README.md

formxtra

formxtra logo

The companion for Web forms!

Extract and set form values through the DOM.

Installation

The package can be found on:

Usage

For an example form:

<button id="autofill" type="button">
  Autofill login form
</button>

<form id="loginForm">
  <input type="text" name="username" />
  <input type="password" name="password" />
  <button type="submit" name="type" value="client">
    Log In As Client
  </button>
  <button type="submit" name="type" value="admin">
    Log In As Admin
  </button>
</form>

<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);
  }
});

const autofillButton = document.getElementById('autofill');

autofillButton.addEventListener('click', e => {
  setFormValues(form, { username: 'admin', remember: true });
});

Tests

The library has been tested on the static DOM using JSDOM, and the real dynamic DOM using Cypress.