Tips for extracting the values of multiple input fields in JavaScript and displaying them on a webpage

I want to collect all the values from input fields and display them on the website.

Check out my code snippet below:

var button = document.querySelector("button");
button.addEventListener("click", function() {

  var inputs = document.querySelectorAll("input").value;
  for (i = 0; i < inputs.length; i++) {
    inputs[i];
  }

  var output = document.getElementById("output");
  output.innerHTML = "Here are the values: " + inputs;
});
<div>
  <input type="number" min="1" max="49">
  <input type="number" min="1" max="49">
  <input type="number" min="1" max="49">
  <input type="number" min="1" max="49">
  <input type="number" min="1" max="49">
  <input type="number" min="1" max="49">
</div>


<button>Check</button>
<div id="output"></div>

Answer №1

When you use the expression

document.querySelectorAll("input").value;
, it does not give you the concatenated values of all selected inputs.

Instead, you can create a new variable and append the values to it inside a for loop like this:

inputsAppended += inputs[i].value;

Then, you can display the combined value using:

output.innerHTML = "Combined Value: " + inputsAppended;

var button = document.querySelector("button");
button.addEventListener("click", function() {
  var inputsAppended = "";
  var inputs = document.querySelectorAll("input");
  for (i = 0; i < inputs.length; i++) {
    inputsAppended += inputs[i].value;
  }

  var output = document.getElementById("output");
  output.innerHTML = "Combined Value: " + inputsAppended;
});
<div>
  <input type="number" min="1" max="49">
  <input type="number" min="1" max="49">
  <input type="number" min="1" max="49">
  <input type="number" min="1" max="49">
  <input type="number" min="1" max="49">
  <input type="number" min="1" max="49">
</div>


<button>Check</button>
<div id="output"></div>

Answer №2

let submitButton = document.querySelector("button");
submitButton.addEventListener("click", function() {

  let numberInputs = document.querySelectorAll("input");
  for (i = 0; i < numberInputs.length; i++) {
  let result = document.getElementById("result");
  
  result.innerHTML += numberInputs[i].value;
  }

  
});
<div>
  <input type="number" min="1" max="49">
  <input type="number" min="1" max="49">
  <input type="number" min="1" max="49">
  <input type="number" min="1" max="49">
  <input type="number" min="1" max="49">
  <input type="number" min="1" max="50">
</div>


<button>Submit</button>
<div id="result"></div>

Answer №3

This method provides a straightforward way to display each number on a separate line:

let button = document.querySelector("button");
button.addEventListener("click", function() {
  // Loop through all input fields, gather their values, and concatenate them with line breaks
  document.getElementById("output").innerHTML =
 [].slice.call(document.querySelectorAll("input")).map( input => input.value ).join( "<br>" );
});
<div>
  <input type="number" min="1" max="49">
  <input type="number" min="1" max="49">
  <input type="number" min="1" max="49">
  <input type="number" min="1" max="49">
  <input type="number" min="1" max="49">
  <input type="number" min="1" max="49">
</div>


<button>Check</button>
<div id="output"></div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Location-based services: Updating the position of a Google Maps marker without refreshing the entire map interface

How can I update only the marker on a map when the device is in motion or has increased accuracy? I want to reload the map when the position changes, but only move the marker. Below is the code snippet that I currently have: if (navigator.geolocation) { ...

React's back button is experiencing functionality issues

I'm having an issue with my quiz page where the previous button is not functioning properly. The user should be able to change their answer before submitting, but after 5-6 clicks on the previous button, they are redirected to the next page without co ...

What is the reason for the failure of this react ternary return statement?

My slideboard is set up to show a warning component (currently just a "test" div) when the prop "columnsItem" exceeds 50. Everything works fine, but when I switch back to a slideboard with fewer columns, all I see is a blank white screen. Can you help me ...

The attribute 'split' is not found on the never data type

I have a function that can update a variable called `result`. If `result` is not a string, the function will stop. However, if it is a string, I then apply the `split()` method to the `result` string. This function always runs successfully without crashin ...

Tips for optimizing large image files on a basic HTML, CSS, and JavaScript website to improve site speed and ensure optimal loading times

Currently, my site is live on Digital Ocean at this link: and you can find the GitHub code here: https://github.com/Omkarc284/SNsite1. While it functions well in development, issues arise when it's in production. My website contains heavy images, in ...

What are the applications of global variables in node.js?

global.test = "test"; console.log(global.test); //test but I want to accomplish this: console.log(test); //test without using: var test = global.test; Is there a way to achieve this? I am looking for a solution where any module in my project does not ...

Creating a PDF from dynamic HTML and transferring it to an AWS S3 bucket without the need to download the PDF

We have created a web application using React JS where we attempted to generate a PDF. Instead of downloading or opening the PDF in a new window, our goal is to directly upload it to an AWS S3 bucket. We have researched and tried various samples but have n ...

Customizing the JavaScript Calendar in Qualtrics

I came across a JavaScript code snippet that allows you to embed a calendar into a Qualtrics question. Specify a date for the survey: <link href="https://cdn.jsdelivr.net/npm/pikaday/css/pikaday.css" rel="stylesheet" type="text/css" /> <script sr ...

Error: The "map" property cannot be read if it is undefined in a Django and React application

While I was following the react and django tutorial, I encountered an issue when I added some code for concern with. The error message 'Cannot read property 'map' of undefined' keeps getting thrown. The error location is pointed out ...

Ways to merge values across multiple arrays

There is a method to retrieve all unique properties from an array, demonstrated by the following code: var people = [{ "name": "John", "age": 30 }, { "name": "Anna", "job": true }, { "name": "Peter", "age": 35 }]; var result = []; people. ...

Bokeh is having trouble loading from the CDN

I am currently attempting to embed a plot along with its data by utilizing autoload_static within a straightforward html page that I wish to view locally on my computer. According to the documentation, all I need to do is place the .js file in the specifie ...

When the button is clicked, redirect to a new page and submit a form using AJAX with data obtained from the click event

I have a page called 2.html where inputting an ID number results in some output being displayed. This functionality is achieved using Ajax post method, sending data along with the data parameter. I also have another page named 1.html that contains a list o ...

Utilize a Python script to transmit data to JavaScript through JSON in order to dynamically alter the content of

Currently, I am developing an interactive display that utilizes sensors on a raspberry pi. The display is set to show a webpage and I have implemented a python script to handle sensor interaction. My goal is to change the displayed web page when a user p ...

Confusing postback phenomena in ASP.NET web forms

I have developed a small script that successfully maintains the focused element during an async postback. However, I encountered an issue when tabbing through controls generated inside an asp:Repeater, where a full postback is unexpectedly triggered. Below ...

React HTML ignore line break variable is a feature that allows developers to

Can you help me with adding a line break between two variables that will be displayed properly in my HTML output? I'm trying to create an object with a single description attribute using two text variables, and I need them to be separated by a line b ...

Creating a script to open multiple URLs in HTML and JavaScript

Currently, I am working on creating a multiple URL opener using HTML and JavaScript. However, I have encountered an issue where HTTP links are opening fine but HTTPS links are not. Can someone provide assistance with this problem? Below is the code snippet ...

Phaser 3 shows images as vibrant green squares

In my project, I have two scenes: Loading and Menu. In the loading scene, I load images with the intention of displaying them in the menu. Here is the code for the Loading scene: import { CTS } from './../CTS.js'; import { MenuScene } from &apo ...

How can I transfer an image from the clipboard onto a fabric.js canvas?

I am currently working on developing a function that will allow users to paste an image from their clipboard onto a canvas as a new fabric.Image(). However, every search result I come across either discusses cloning existing objects within the canvas or pa ...

Preparing JSON data for creating a wind map with Leaflet

I am currently working on converting netCDF data to JSON in order to use it with leaflet-velocity. This tool follows the same format as the output of grib2json used by cambecc in earth. An example of sample JSON data can be found at wind-global.json. By u ...

Adding local JavaScript to a Vue component is a great way to enhance its functionality

I am currently working on integrating a homepage concept (Home.vue) into my project. The design is based on a template that I purchased, which includes CSS, HTML files, and custom JavaScript. While most of the CSS has been successfully imported, I am havin ...