I want to create from user input two arrays while calling a function. The issue is that I get an extra value in the array that I did not type in; it’s looks like a default value...
I have the following code:
const NameArr = [];
const IdArr = [];
function getName() {
const Input = document.getElementById("input").value;
const list_names = Input.split(", ");
for (let i of list_names) {
NameArr.push(i);
IdArr.push(i);
};
console.log(NameArr, IdArr);
return NameArr, IdArr;
};
getName();
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<script src="main.js" charset="utf-8" defer></script>
<title></title>
</head>
<body>
<form id="form" action="#" method="post">
<input type="text" id="input">
<button onclick="getName();">Get Data</button>
</form>
</body>
</html>
I am not sure why I get an extra value [""] in my arrays...
return NameArr, IdArr;supposed to do?getName();inside the global script adds an extra""to both, globally defined arrays. Remove that line, it looks like a left over from something.HTMLwith the<button onclick="getName();">Get Data</button>, right?