const studentName = [];
function AddNewMember(){
for(var i=0; i<4 ; i ){
var num1 = document.getElementById("vorodi").value;
studentName.push(num1);
}
}
I have an input from which I want to receive five names. You can't do this with this code. What is the solution to this problem?
CodePudding user response:
you can use this solution
let num1 = document.getElementById("vorodi");
function AddNewMember(){
let name = num1.value;
studentName.push(name);
num1.value = "";
console.log(studentName);
}
CodePudding user response:
As I understand you want to take value of multiple inputs in (your case it is 4)
You can try below one "vorod" i
concat value of i
with static text
const studentName = [];
function AddNewMember(){
alert("called")
for(var i=0; i<4 ; i ){
var num1 = document.getElementById("vorod" i).value;
studentName.push(num1);
}
console.log(studentName)
}
<input type="text" id="vorod0"><br><br>
<input type="text" id="vorod1"><br><br>
<input type="text" id="vorod2"><br><br>
<input type="text" id="vorod3"><br><br>
<input type="button" value="Add Member" onclick="AddNewMember()">
CodePudding user response:
I don't know exactly what you are trying to do but if you want to get mutiple elements from your document(assuming you are working with a html page) you can use the getElementsByClassName() or getElementsByTagName() functions but you have to put the tag or class in your html element.