0

I've come across this nifty little function, to add a hidden input field to an HTML form with JavaScript.

Here's the code:

function addHidden(theForm, key, value) {
    // Create a hidden input element, and append it to the form:
    var input = document.createElement('input');
    input.type = 'hidden';
    input.name = key;'name-as-seen-at-the-server';
    input.value = value;
    theForm.appendChild(input);
}

// Form reference:
var theForm = document.forms['detParameterForm'];

// Add data:
addHidden(theForm, 'key-one', 'value');
addHidden(theForm, 'another', 'meow');
addHidden(theForm, 'foobarz', 'baws');

// Submit the form:
theForm.submit();

What I do not understand is the 'name-as-seen-at-the-server' in input.name = key;'name-as-seen-at-the-server';.

What exactly does this set and how is it used?

Community
  • 1
  • 1
SaAtomic
  • 619
  • 1
  • 12
  • 29
  • 1
    In context given (a function that appends inputs to DOM), it is nothing. The key can be any arbitrarily determined string. – zer00ne Mar 13 '17 at 07:09
  • 1
    I ran it on jsfiddle and `'name-as-seen-at-the-server';` does nothing. Then value os `key` is just assigned to the variable on the left. – Ayush Mar 13 '17 at 07:10
  • 1
    Comment out name-as-seen-at-the-server or delete it, it's just your field name. nothing else – Omar Mar 13 '17 at 07:12

1 Answers1

3

It's likely just describing the key input. Comment it out or remove it, and all should work just fine. Change the line input.name = key;'name-as-seen-at-the-server'; to input.name = key;//'name-as-seen-at-the-server';

berko
  • 383
  • 2
  • 4
  • So this will really just create the input field with the name `key` and the value `value`, right? thank you! I just tried it with jsbin, it works just fine - thanks! – SaAtomic Mar 13 '17 at 07:12