Solution 1 :

When you run this in a browser, the following error is reported in the console when you click the Get button:

Uncaught ReferenceError: id is not defined
    at HTMLInputElement.button.onclick (test.html:53)

Ignore the error for now, you can see that the error in actually in the second function, but for Get you were probably expecting the first funtion. To fix this issue, do not assign the second function, at least not to the button you have selected.

Notice how you have named both buttons the same name, this will make them hard to target, but also you are not using that name in the querySelector. So lets change that first, give each button a unique name and use it to select each button:

    <input type="button" name="getButton" value="Get"><br>
    <input type="button" name="addButton" value="Add">
  let getButton = document.querySelector('.textFields input[name="getButton"]');
  let addButton = document.querySelector('.textFields input[name="addButton"]');
...
getButton.onclick = ...
...
addButton.onclick = ...

Now, when you click on the Get button there is no error, and it appears to function as you have described, clicking Add still raises the original error.

You have used a variable called id but you have not yet declared what that variable is yet. I would assume you probably want to make it ‘textX’ where x is the next number.

So add the following lines inside the button click function to declare the Id:

You need to put this logic inside the function because you need it to be re-evaluated each time the button is clicked. Other valid solutions would include incrementing the value instead or re-querying for x, but this will work.

let x = document.querySelectorAll('.textFields .text');
let id = 'text' + (x.length + 1);

Save and Run, you will see the next issue in the console:

Uncaught ReferenceError: container is not defined
    at HTMLInputElement.addButton.onclick

As with id, you have not defined the variable container, here I will again assume you meant to reference the .textFields div, so following your querySelector style, we can create a variable called container:

let container = document.querySelector('.textFields');

That will start appending your text boxes to the page, but they are still not being picked up by the Get button.

Another assumption here, but you have assigned a class .textResult to the new texboxes. If instead you assigned the class .text to them, then you would almost pick them up in the selector

textField.classList.add("text");

The reason that they aren’t picked up is back to where the value of x is evaluated that the Get button is using. Because it is evaluated the first time in the main script, but never re-evaluated when the button is clicked the new text boxes are not included in the array stored in x.

As with the advice above for requerying x to get the updated count, Simply fix this by moving the line to initialise x into the first function.

Overall, your page with the embedded script could not look something like this:

<html>
<body>

<form>
    <div class="textFields">
    <label for="text1">text1:</label><br>
    <input type="text" class="text" name="text1"><br>

    <label for="text2">text2:</label><br>
    <input type="text" class="text" name="text2"><br>

    <label for="text3">text3:</label><br>
    <input type="text" class="text" name="text3"><br>

    <label for="text4">text4:</label><br>
    <input type="text" class="text" name="text4"><br>

    <label for="text5">text5</label><br>
    <input type="text" class="text" name="text5"><br>

    <label for="text6">text6</label><br>
    <input type="text" class="text" name="text6"><br>

    <input type="button" name="getButton" value="Get"><br>
    <input type="button" name="addButton" value="Add">

    <br>

    <label for="textResult">Text Result</label><br>
    <input type="text" id="textResult" name="textResult"><br>


    </div>
    </form>


   <script>

  
  let getButton = document.querySelector('.textFields input[name="getButton"]');
  let addButton = document.querySelector('.textFields input[name="addButton"]');
  let result = document.querySelector('#textResult');
  let container = document.querySelector('.textFields');

 getButton.onclick = function() {
 
 let x = document.querySelectorAll('.textFields .text');
   result.value = '';
    for (i = 0; i < x.length; i++) {
    result.value += x[i].value + ' ';
     }
  }

addButton.onclick = function() {
let x = document.querySelectorAll('.textFields .text');
var textField = document.createElement("INPUT")  
let id = 'text' + (x.length + 1);
    textField.setAttribute("id", id)
    textField.setAttribute("name", id)
    textField.classList.add("text")
    container.appendChild(textField) 

}

</script>
</body>
</html>

Have a look at some of the guidance in this post for further simple examples: How do I add textboxes dynamically in Javascript?

Problem :

This program has 6 text fields and when a user inputs into the text fields, the text result box will concatenate the input text. I am struggling to get a button to work which will add a 7th text field and then also add the user input together. I have tried to append it but not sure where I am going wrong.

 <html>
 <body>

<form>
    <div class="textFields">
    <label for="text1">text1:</label><br>
    <input type="text" class="text" name="text1"><br>

    <label for="text2">text2:</label><br>
    <input type="text" class="text" name="text2"><br>

    <label for="text3">text3:</label><br>
    <input type="text" class="text" name="text3"><br>

    <label for="text4">text4:</label><br>
    <input type="text" class="text" name="text4"><br>

    <label for="text5">text5</label><br>
    <input type="text" class="text" name="text5"><br>

    <label for="text6">text6</label><br>
    <input type="text" class="text" name="text6"><br>

    <input type="button" name="button" value="Get"><br>

    <input type="button" name="button" value="Add">

    <br>

    <label for="textResult">Text Result</label><br>
    <input type="text" id="textResult" name="textResult"><br>


    </div>
    </form>


   <script>

  let x = document.querySelectorAll('.textFields .text');
  let button = document.querySelector('.textFields input[type="button"]');
  let result = document.querySelector('#textResult');

 button.onclick = function() {
   result.value = '';
    for (i = 0; i < x.length; i++) {
    result.value += x[i].value + ' ';
     }
  }

button.onclick = function() {
var textField = document.createElement("INPUT")  
    textField.setAttribute("id", id)
    textField.setAttribute("name", id)
    textField.classList.add("textInput")
    container.appendChild(textField) 

}


</script>


</body>
</html>

Comments

Comment posted by David

“but not sure where I am going wrong”

Comment posted by Chris Schaller

There is a clear error here

Comment posted by WILLIAM INGLE

Hi, I am using BBEdit, the button itself comes onto the website, but doesn’t work, no errors are coming from the environment. Everything else works apart from the button which I need to use to add a 7th text field. I am using document.createElement for this and then trying to append it.

Comment posted by Chris Schaller

Run it is a browser then with dev tools turned on, have a look at the console, there are a few simple syntax and grammatical issues to address.

Comment posted by WILLIAM INGLE

I see the errors now, it also seems that adding the extra button has stopped my “get” button from working

Comment posted by WILLIAM INGLE

Hi, thanks for you reply. I have gone through the code you explained and inputted it into my program. I now see the issues I had before. Its still coming up with a few errors which I am trying to fix.

Comment posted by Chris Schaller

If it helps, kindly mark this as the answer, if you need further assistance we could take this into a chat.

Comment posted by WILLIAM INGLE

Hi, could we take this into a chat? I am not sure how to though as i am new to stackOverflow

Comment posted by Chris Schaller

I’ve updated my post with the entire page that I tested with, I hope that helps, i can’t detect any errors in it, so compare this with yours to identify what might be different. Otherwise you’ll need to provide the error message that you see in the console window

Comment posted by WILLIAM INGLE

Okay I have now seen the issue in mine and resolved it. The main part I want to change is making the new text box have the title textbox7 on it like the others, and make it before the buttons

By