lang/js

VanillaJS Form serialize()

C/H 2019. 12. 16. 11:17
<form method="dialog">
  <p><label>Favorite animal:
    <select name="favAnimal" required>
      <option></option>
      <option>Brine shrimp</option>
      <option>Red panda</option>
      <option>Spider monkey</option>
    </select></label>
  </p>
  <p>
    <label>
      <input type="text" name="animallName" placeholder="animal name" value="Tiger" />
    </label>
  </p>
  <menu>
    <button>Cancel</button>
    <button>Confirm</button>
  </menu>
</form>
Array.from(document.querySelector('form')).map(item=>item.name?`${item.name}=${item.value}`:null).filter(item=>!!item).join('&')
// "favAnimal=&animallName=Tiger"
반응형

'lang > js' 카테고리의 다른 글

Javascript EventTarget()  (0) 2019.12.18
Canvas Tutorials  (0) 2019.12.17
JS Function.method  (0) 2019.12.11
JS Array.Method  (0) 2019.12.10
JS Array.prototype.reduce()  (0) 2019.12.09