I'm trying to use objects so a number of people will fill their information and it will save every object in an array. But, every time I try to change the text of my HTML elements, I get this error:
Uncaught TypeError: Cannot set property 'innerHTML' of null
here is my code:
<html>
<head>
<title></title>
</head>
<body>
<p id="demo">hello there</p>
<script type="text/javascript">
var first_name;
var last_name;
var age;
var eye_color;
var favorite_color;
var NumOfPeople=0; //note:NumOfPeople is one smalller than the actual num of people
var persons= new Array();
var x;
var y;
function GetInfo(){
//document.body.innerHTML = '';
x= NumOfPeople+1;
document.getElementById("demo").innerHTML = "Paragraph changed!";
document.write("<input type= 'text' id= 'FirstName' value='your first name'/>");
document.write("<input type= 'text' id= 'LastName' value='your last name' />");
document.write("<input type= 'text' id= 'Age' value='your age'/>");
document.write("<input type= 'text' id= 'EyeColor' value='your eye color' />");
document.write("<input type= 'text' id= 'FavoriteColor' value='your favorite color' />");
document.write("<input type= 'button' id= 'sumbit1' value='sumbit' onclick='submitInfo()' />");
}
function person(first, last, age, eye, color){
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eye;
this. favoriteColor= color;
};
function submitInfo(){
first_name= document.getElementById("FirstName").value;
last_name=document.getElementById("LastName").value;
age=document.getElementById("Age").value;
eye_color=document.getElementById("EyeColor").value;
favorite_color=document.getElementById("FavoriteColor").value
persons[NumOfPeople]=new person(first_name, last_name, age, eye_color, favorite_color);
NumOfPeople++;
GetInfo();
}
GetInfo();
</script>
</body>
element
– user5837410 Apr 04 '16 at 15:44