Input not being added to array (beginner) JavaScript

WiseEye

When text is written into the input box and then the button is clicked, I want the text (value) to be added to the array I have created for it (the array is called 'subject'). The text (value) from the input box is not being added... why?

var subject = [];
function addSubjects() {
  namesInputted = document.getElementById('namesInputter').value;
  subject.push(namesInputted);
  console.log(subject);
}
<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
  <meta charset="utf-8">
  <title>Prueva tu Suerte</title>
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
  <link rel="stylesheet" href="styles.css">
</head>

<body>
  <form class="" action="index.html" method="post">
    <input type="text" id="namesInputter" value="">
    <button onclick="addSubjects()">Add Player</button>
  </form>
  <span id='S'></span>
  <span id='V'></span>
  <span id='O'></span>
  <span id='P'></span>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="function.js"></script>
</body>

</html>

BadPiggie

Actually, You code is working. The input value is appending to the array. But the Issue is, You are using form. So when you click the button, It will submit the form right after add the value to array. So page will refresh and the array will become empty.

Just remove form tag, if you don't want to submit data.

var subject = [];
function addSubjects() {
  namesInputted = document.getElementById('namesInputter').value;
  subject.push(namesInputted);
  console.log(subject);
}
<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
  <meta charset="utf-8">
  <title>Prueva tu Suerte</title>
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
  <link rel="stylesheet" href="styles.css">
</head>

<body>
  <input type="text" id="namesInputter" value="">
  <button onclick="addSubjects()">Add Player</button>
  <span id='S'></span>
  <span id='V'></span>
  <span id='O'></span>
  <span id='P'></span>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="function.js"></script>
</body>

</html>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Elements not being added to my Array Lists

Pure beginner javascript - array and map

q-field-no-input being added to fields

Why is VueJS detecting this property being added to an object in an array?

Why is not the value added from input fields to the array?

Looping through API XML data is not being added to the array correctly

from php array to javascript (beginner)

Add attribute to the recently added input field in javascript

JavaScript - Array item not being found

How to await values being added to array?

specifying the index of new elements being added to an array via a loop javascript

Why strings are not being added to array in JavaScript?

Beginner in JavaScript

Value not being added to array in PHP foreach statement

objects not being added to an array - swift iOS

Prevent duplicate ids from being added to session (array)

Negative value not being added to Array

How to decrease text size with more input being added in textview?

Array not being added to variable in foreach loop

stopping the no results label being added to input and if input matches a label

Input not being read by code in JavaScript

Size of object array is being added to the localstorage not the array of objects

Remove item from being added to an array or remove an item from an array

Output of array not correct extra letters being added

Extra elements being added to an array

Select input field options being added outside select field

JavaScript Input Field Not Being Recognized

Unable to return array values after being added into the array

Array not being added to Firebase