I was trying out some things to learn about how to work with constructors and objects. I wanted to create a page to show the input from the textfields using constructors and objects in Javascript. When I click on save it shows [object Object] on the HTML page. I tried some things but that failed.
function Persons(name, age){ this.name = name; this.age = age; } var button = document.querySelector('#save'); function showPerson4(){ var name = document.querySelector("#name"); var age = document.querySelector("#age"); var person4 = new Persons(); person4.name = name; person4.age = age; document.querySelector("#person4").innerHTML = person4; } button.addEventListener("click", function (ev) { ev.preventDefault(); showPerson4(); },false); <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Show person</title> </head> <body> Name: <input type="text" id="name" name="name" placeholder="Enter your name"><br> Age: <input type="text" id="age" name="age" placeholder="Enter your age"> <button id="save" name="save">Save</button> <p id="person4" class="person4"></p> <script src="js/opdracht1.js"></script> </body> </html>
Personsconstructor that expects to be passedname, age. Why don't you use it?