0

I'm trying to get user input from an html form into an array, by using getElementsByClassName.

It works using getElementsById("...").value and then push it into the empty array. But when I try to do it with getElementsByClassName, I get a htmlcollection returned and something seems to go wrong. It doesn't register the user input. Any help is strongly appreciated, I've been trying to find a solution all day...

<title>Word Input</title>

<form> 

    <input type="text" class="englishWord"> <input type="text" class="spanishWord"> <br> <br> 
    <input type="text" class="englishWord"> <input type="text" class="spanishWord"> <br> <br> 
    <input type="text" class="englishWord"> <input type="text" class="spanishWord"> <br> <br> 
    <input type="text" class="englishWord"> <input type="text" class="spanishWord"> <br> <br> 
    <input type="text" class="englishWord"> <input type="text" class="spanishWord"> <br> <br> 


    <button id="submit"> Submit </button>
</form>

<script> 

    document.getElementById("submit").addEventListener("click", handler9); 

    function handler9() {

    let vocabEnglish = [];
    let englishWords = document.getElementsByClassName("englishWord");


    for (let i = 0; i < englishWords.length; i++) {
            let englishWord = englishWords[i].innerText;
            vocabEnglish.push(englishWord); 
        }
    }

    console.log(vocabEnglish); 

</script>

I expect the words to be pushed into an array, but I get returned an empty one.

3 Answers 3

2

A few issues:

  • Your indentation is off, and putting it right reveals that the console.log happens outside of the function that has the local variable vocabEnglish. So obviously it is undefined outside the function.
  • The value of an input element is not retrieved via the innerText property, but value.
  • When you click the button the form is submitted, and a reload of the page happens, whereby you lose the output. Cancel the submission with e.preventDefault

Corrected code:

document.getElementById("submit").addEventListener("click", handler9);

function handler9(e) {
    let vocabEnglish = [];
    let englishWords = document.getElementsByClassName("englishWord");

    for (let i = 0; i < englishWords.length; i++) {
        let englishWord = englishWords[i].value;
        vocabEnglish.push(englishWord); 
    }
    console.log(vocabEnglish);
    e.preventDefault()
}
Sign up to request clarification or add additional context in comments.

1 Comment

I can't thank you enough! I didn't know about the reloading of the page after submitting a form. It would have taken me a long time to figure that one out..
1

use value instead of innerText

I've made an example here, it still prints to the console for you to view.

document.getElementById("submit").addEventListener("click", handler9);

function handler9() {
  let vocabEnglish = [];
  let englishWords = document.getElementsByClassName("englishWord");

  for (let i = 0; i < englishWords.length; i++) {
    let englishWord = englishWords[i].value;
    vocabEnglish.push(englishWord);
  }
  console.log(vocabEnglish);
}
<form>

  <input type="text" class="englishWord"> <input type="text" class="spanishWord"> <br> <br>
  <input type="text" class="englishWord"> <input type="text" class="spanishWord"> <br> <br>
  <input type="text" class="englishWord"> <input type="text" class="spanishWord"> <br> <br>
  <input type="text" class="englishWord"> <input type="text" class="spanishWord"> <br> <br>
  <input type="text" class="englishWord"> <input type="text" class="spanishWord"> <br> <br>


  <button id="submit"> Submit </button>
</form>

Comments

0
    <form> 

    <input type="text" class="englishWord"> <input type="text" class="spanishWord"> 
    <br> <br> 
   <input type="text" class="englishWord"> <input type="text" class="spanishWord"> 
   <br> <br> 
   <input type="text" class="englishWord"> <input type="text" class="spanishWord"> 
     <br> <br> 
    <input type="text" class="englishWord"> <input type="text" class="spanishWord"> 
    <br> <br> 
<input type="text" class="englishWord"> <input type="text" class="spanishWord"> <br> <br> 


<button id="submit"> Submit </button>
</form>

<script> 

document.getElementById("submit").addEventListener("click", handler9); 

function handler9() {

let vocabEnglish = [];
let englishWords = document.getElementsByClassName("englishWord");


for (let i = 0; i < englishWords.length; i++) {
        let englishWord = englishWords[i].value;
        vocabEnglish.push(englishWord); 
    }
}

console.log(vocabEnglish); 

</script>

1 Comment

Good answer! The only thing missing is some explanation so that he can understand why it works.

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.