Javascript Form How to - Get form elements as associative array








Question

We would like to know how to get form elements as associative array.

Answer


<!DOCTYPE html>
<html>
<head>
</head><!--from  w  w w . java  2  s  .c o m-->
<body>
  <form name="ff" action="" method="POST">
    <input name="student[john]" value="John"> 
    <input name="student[kofi]"> 
    <input name="student[kwame]"> 
    <input type="submit" name="submit">
  </form>
  <a onclick="console.log(document.ff['student[john]'].value);">click me <a>
</body>
</html>

The code above is rendered as follows: