0

Lets say I have a form:

<form>
  <input type="text" class="blanked">
  <input type="text" class="blanked">
  <input type="text" class="blanked">
  <input type="submit" value="Press Me">
</form>

Now lets say I use getElementsByClassName to create an array of these elements:

let filledBlanks = Array.from(document.getElementsByClassName( 'blanked' ));

Now I want to iterate through the values the user has entered into each input:

filledBlanks.forEach( filledBlank => {
  let inputText = filledBlank.???;
} 

This is where I get tripped up. What do I use to get the filled value (not .value)? Or is there a better way to accomplish this?

4
  • 3
    Why would it not be filledBlank.value? Commented Nov 5, 2019 at 17:31
  • 1
    "What do I use to get the filled value (not .value)" Why? Commented Nov 5, 2019 at 17:33
  • You can use innerHTML for this Commented Nov 5, 2019 at 17:35
  • Thanks everyone for the feedback. .value wasn't working for me initially, which is why I said not .value above...but after hearing that it should work I went back and realized my JS was running when the page initially loaded and not with the onclock event thus why value had been returning empty values for me previously. Commented Nov 5, 2019 at 17:53

1 Answer 1

1

the .value property works just fine. I don't see why you would need something else. If you want to only iterate over the values, you could map your input first, but i don't see the point.

Please note here that i'm only using Jquery to listen to the submit event. The rest is pure vanilla Javascript.

$('form').on('submit', (event) => {
  event.preventDefault();
  
  let filledBlanks = Array.from(document.getElementsByClassName( 'blanked' ));
  filledBlanks.map((input) => {
    return input.value;
  }).forEach((value) => {
    console.log(value);
  });
  
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form>
  <input type="text" class="blanked">
  <input type="text" class="blanked">
  <input type="text" class="blanked">
  <input type="submit" value="Press Me" id="submit">
</form>

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

1 Comment

Thanks Nicolas. As I mention in the comments above, the issue was elsewhere in my code. I had the JS outside of a function and it was running on page load rather than after the submit button had been click, so it was always returning empty...thus why I thought value didn't work.

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.