-1

I want to save values from inputs in the form into variables
Like this example.

<form action="test3" method="POST">
      Activity name: <input type="text" name="fname" id='nm'><br>
      input1: <input type="text" name="input1" id='in1'><br>
      input2: <input type="text" name="input2" id='in2'><br>
      output1: <input type="text" name="output1" id='out1'><br>
      output2: <input type="text" name="output2" id='out2'><br>
      <input type="submit" value="Submit">
    </form>
    var valname=document.getElementById("nm").value;
    var valin1=document.getElementById("in1").value;
    var valin2=document.getElementById("in2").value;
    var valout1=document.getElementById("out1").value;
    var valout2=document.getElementById("out2").value;

Now, how can i get value into a general variable like : var valeur=valin1;

Maki
  • 1
  • 1
  • 5
  • I assume he means serialising @prasad – Ian May 02 '17 at 11:40
  • Possible duplicate of [form serialize javascript (no framework)](http://stackoverflow.com/questions/11661187/form-serialize-javascript-no-framework) – Ian May 02 '17 at 11:41

1 Answers1

2

You could use FormData

var form = document.querySelector('form'); var data = new FormData(form);

Best off adding an ID to the form though and getById as if you had multiple forms on a page, this wouldn't solve it.

Mozilla API Docs

Ian
  • 3,539
  • 4
  • 27
  • 48