1

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>

2 Answers2

3

You are selecting the element but not the value. Update to:

var name = document.querySelector("#name").value;
var age = document.querySelector("#age").value;

I've used prototype.toString to override the toString() method of the Persons

function Persons(name, age){
    this.name = name;
    this.age = age;        
}

// override the persons.toString
Persons.prototype.toString = function personToString()
{
    return 'Name: ' + this.name + ', Age: ' + this.age;
}

var button = document.querySelector('#save');  

function showPerson4(){
    var name = document.querySelector("#name").value;
    var age = document.querySelector("#age").value;

    var person4 = new Persons(name, age);
                                                   // call toString()
    document.querySelector("#person4").innerHTML = person4.toString();
}

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>
haldo
  • 14,512
  • 5
  • 46
  • 52
0

You just made two mistakes

1) You assigned the variables name and age the input controls itself not their values

 var name   = document.querySelector("#name").value;
 var age = document.querySelector("#age").value;

2) To display Javascript object in html you have to convert it to string JSON.stringify() will do the trick

function Persons(name, age){
    this.name   = name;
    this.age = age;
}

var button  = document.querySelector('#save');

  

function showPerson4(){

    //get the value from inputs
    var name   = document.querySelector("#name").value;
    var age = document.querySelector("#age").value;

    var person4    = new Persons();
    person4.name  = name;
    person4.age = age;

//Stringify results for viewing object in html
    document.querySelector("#person4").innerHTML = JSON.stringify(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>