Click here to Skip to main content
15,884,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have three input fields and one button
1. text
2. time
3. text

BUTTON

These names are: array[]

I want their data to be put into one variable "inputValue" when the click was on the button.

But when I console.log it gives "undefined".

Somebody?

What I have tried:

HTML
<!DOCTYPE html>
<html>
<head>
    <meta charset='utf-8'>
    <meta http-equiv='X-UA-Compatible' content='IE=edge'>
    <title>Value undefined</title>
    <meta name='viewport' content='width=device-width, initial-scale=1'>
</head>
<body>
    <div>
        <h3>To Do </h3>
        <input type="text" id="myInput" name="array[]" placeholder="Write here...">
        <h3>It's time: </h3>
        <input type="time" id="myInput_time" name="array[]" placeholder="Write here...">
        <h3>Description: </h3>
        <input type="text" id="myInput_description" name="array[]" placeholder="Write here...">
        <button onclick="getInputs()" class="addBtn">ADD</button>
    </div>
    <script>
        function getInputs(){
            let inputValue = document.getElementsByName("array[]").value
            console.log(inputValue)
        }
    </script>
</body>
</html>
Posted
Updated 23-Jul-22 7:17am

1 solution

JavaScript
let inputValue = document.getElementsByName("array[]").value
You cannot do that. You have to enumerate the list of controls returned by getElementsByName and get the .value of each control separately, appending that value to your result string.
JavaScript
let inputs = document.getElementsByName("array[]");
inputs.forEach(c => inputValue = inputValue + ";" + c.value);

But, it would be better to scrap the "array[]" name for every input and just use individual names for each input, preferably names that describe what the input is for.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900