1

I need to have 5 text fields, and after the user inputs text in there, i need to put the text together into a text field called Text Result.

This is my code so far but I am unsure on how to continue.

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

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

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

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

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

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

    </form>

    <script>

    function myFunction() {
      var x = document.getElementsByClassName("textFields");
      var i;
       for (i = 0; i < x.length; i++) {
  




      </script>

3
  • 1
    It looks like you only posted part of your JS function Commented Jan 13, 2021 at 15:40
  • I have posted everything I have done so far, I am just stuck on how to continue Commented Jan 13, 2021 at 15:42
  • We're not a code writing service, we're a help you fix your broken code site. That kinda mean you need some broken code for us to help you with. For a suggestion on how to proceed, i'd try logging each element in the result from getElementsByClassName to the console so you can examine it contents. Commented Jan 13, 2021 at 17:30

3 Answers 3

1

getElementsByClassName is not as useful as querySelectorAll

You can delegate

I added a class to the input fields and wrapped them in their own div

window.addEventListener("load", function() { // on page load
  const container = document.querySelector(".textFields");
  const result = document.getElementById("textResult");
  const addButton = document.getElementById("add");
  const textFields = container.getElementsByClassName("textInput"); // getElementsByClassName is a LIVE collection
  container.addEventListener("input", function() {
    const res = [...textFields] // make the nodelist an array 
      .map(fld => fld.value) // return the value
      .filter(val => val.trim() !== "") // only take actual content
      .join(", "); // .join (""); // join on ", " or ""
    result.value = res;
  });
  addButton.addEventListener("click", function() {
    const lastNumber = textFields.length; // remember it is a LIVE list 
    const id = "text" + (lastNumber + 1); // your IDs start at 1
    const label = document.createElement("label")
    label.setAttribute("for", id)
    label.textContent = id;
    container.appendChild(label)
    container.appendChild(document.createElement("br"))
    const textField = document.createElement("input")
    textField.setAttribute("id", id)
    textField.setAttribute("name", id)
    textField.classList.add("textInput")
    container.appendChild(textField)
  });
});
<form>
  <button id="add" type="button">Add</button>
  <div class="textFields">
    <label for="text1">text1:</label><br>
    <input type="text" id="text1" name="text1" class="textInput"><br>

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

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

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

    <label for="text5">text5</label><br>
    <input type="text" id="text5" name="text5" class="textInput"><br>
  </div>
  <label for="textResult">Text Result</label><br>
  <input type="text" id="textResult" name="textResult"><br>
</form>

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

8 Comments

That works perfectly, but I have to use Javascript for it, I am quite new to javascript.
it IS JavaScript
Thankyou, after looking at it i have a better understanding of it now
With that code, will still work to add in a button that adds a 6th text field in
Ill try and get that working using createElement()
|
1

I made a decision for you with a button, by clicking on which you will receive the entered values.

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 + ' ';
    }
}
<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>
    
    <input type="button" name="button" value="get"><br>

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

1 Comment

So could this code be used to add a button to it aswell to add a seventh text field? Curious to how that would work with how you used a for loop with the code, would be good for experience to see how it is done
0

you can try using concat() method. The concat() method is used to join two or more strings. example:

var str1 = "Hello ";
var str2 = "William";
var str3 = " How are you doing!";
var res = str1.concat(str2, str3);

1 Comment

The text fields will have user input in them, I was hoping to use the getElementsByClassName function

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.