0

I found this code on here (thanks to Xavi López) and it is ideal for what I need to add to my project but I'm in need of some help adding a Form post and submit button in JavaScript. I have no knowledge on this subject and I've tried looking at some example but non of them seem to work. I would be grateful if someone could help me. After the user adds the relevant number of input boxes and adds there data, I would like to have a submit button which will POST the results to another web page (result page)

I have added the solution to the below coding (thank you MTCoster) but I'm now try to find a solution to having the submit button appear only when an entry has been added. I have tried different methods but non will work.

function addFields() {
  // Number of inputs to create
  var number = document.getElementById('member').value;
  
  // Container <div> where dynamic content will be placed
  var container = document.getElementById('container');
  
  // Clear previous contents of the container
  while (container.hasChildNodes()) {
    container.removeChild(container.lastChild);
  }
  
  for (i = 0; i < number; i++) {
    // Append a node with a random text
    container.appendChild(document.createTextNode('Member ' + (i + 1) + ' '));
    
    // Create an <input> element, set its type and name attributes
    var input = document.createElement('input');
    input.type = 'text';
    input.name = 'member' + i;
    container.appendChild(input);
    
    // Append a line break 
    container.appendChild(document.createElement('br'));
  }
}
<input type="text" id="member" name="member" value="">Number of Pins: (max. 48)<br>
<a href="#" id="filldetails" onclick="addFields()">Add Pinout Entries</a>
<form action="result.asp" method="POST">
<div id="container"></div>
<input type="submit" value="Add Data">
</form>

1 Answer 1

1

You’re almost there - all you need to do is wrap your inputs in a <form> element:

function addFields() {
  // Number of inputs to create
  var number = document.getElementById('member').value;
  
  // Container <div> where dynamic content will be placed
  var container = document.getElementById('container');
  
  // Clear previous contents of the container
  while (container.hasChildNodes()) {
    container.removeChild(container.lastChild);
  }
  
  for (i = 0; i < number; i++) {
    // Append a node with a random text
    container.appendChild(document.createTextNode('Member ' + (i + 1) + ' '));
    
    // Create an <input> element, set its type and name attributes
    var input = document.createElement('input');
    input.type = 'text';
    input.name = 'member' + i;
    container.appendChild(input);
    
    // Append a line break 
    container.appendChild(document.createElement('br'));
  }
}
<input type="text" id="member" name="member" value="">Number of Pins: (max. 48)<br>
<a href="#" id="filldetails" onclick="addFields()">Add Pinout Entries</a>
<form action="/url/to/post/to" method="POST">
  <div id="container"></div>
  <input type="submit">
</form>

If you’d like the submit button to only appear after at least one input is visible, you could add it at to div#container at the end of addFields(). I’ll leave this as an exercise to the OP, since it’s not much different to how you’re adding the input fields.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for a quick answer. Could you elaborate on how you would have it appear after one input has been selected, please
Try it yourself - as I said in the answer, it’s not much different to how you’re adding the other <input>s. It’ll work just fine added inside the #container rather than after it as I’ve put it.
Sorry I'm not to good on this. I'm trying to understand how to add it inside the #container If I try <form action="result.asp" method="POST"><div id="container"><input type="submit"></div> the submit button disappears when I add the entries.
Right, that’s why if you hardcode it it should be outside the #container. But if you’re dynamically adding it, do that the same way you’re adding the other inputs
Would that mean the form submit button would need to been written in JavaScript so that when the user adds an entry it will only appear then?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.