Extract and set form values through the DOM—no frameworks required! https://github.com/TheoryOfNekomata/formxtra
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # formxtr
  2. Extract form values through the DOM.
  3. ## Installation
  4. The library is not yet out on any package manager. Installing through the URL is the preferred way.
  5. ## Usage
  6. For an example form:
  7. ```html
  8. <!-- ... -->
  9. <form id="form">
  10. <input type="text" name="username" />
  11. <input type="password" name="password" />
  12. <button type="submit" name="type" value="client">Log In As Client</button>
  13. <button type="submit" name="type" value="admin">Log In As Admin</button>
  14. </form>
  15. <!-- ... --->
  16. ```
  17. Use the library as follows (code is in TypeScript, but can work with JavaScript as well):
  18. ```typescript
  19. import getFormValues from '@theoryofnekomata/formxtr';
  20. const form: HTMLFormElement = document.getElementById('form');
  21. // optional, but just in case there are multiple submit buttons in the form,
  22. // individual submitters can be considered
  23. const submitter = form.querySelector('[type="submit"][name="type"][value="client"]');
  24. const values = getFormValues(form, submitter);
  25. const processResult = (result: Record<string, unknown>) => {
  26. throw new Error('Not yet implemented.');
  27. };
  28. // Best use case is with event handlers
  29. form.addEventListener('submit', async e => {
  30. const { target: form, submitter } = e;
  31. e.preventDefault();
  32. const values = getFormValues(form, submitter);
  33. // Get the form values and send as request to some API
  34. const response = await fetch(
  35. 'https://example.com/api/log-in',
  36. {
  37. method: 'POST',
  38. body: JSON.stringify(values),
  39. headers: {
  40. 'Content-Type': 'application/json',
  41. },
  42. },
  43. );
  44. if (response.ok) {
  45. const result = await response.json();
  46. processResult(result);
  47. }
  48. })
  49. ```
  50. ## Tests
  51. The library has been tested on JSDOM through Jest, and the real DOM using Cypress.