2

window.onload = function() {

  var Employee = function(name, bd) {
    this.name = name;
    this.bd = bd;
    this.age = function() {
      2017 - this.bd;
    }
  }

  var empName = document.getElementById("name").value;
  var empBday = document.getElementById("bday").value;

  var empList = new Employee(empName, empBday);

  var Btn = document.getElementById('add').addEventListener('click', function() {
    console.log(empList);
  });
}
<input type="text" id="name" value=""></input>
<input type="text" id="bday" value=""></input>
<button id="add">Add</button>

Well even after i input some values i then Console.log the object called "emplist" but i only get Employee {name: "", bd: "", age: ƒ}

3
  • When the window loads, the values of the inputs are just empty strings, so that's what you get. Did you expect something else ? Commented Aug 25, 2017 at 11:45
  • You set the values on window.onload. You have to update them upon changes in the inputs developer.mozilla.org/fi/docs/Web/Events/change They are empty when the page is loaded. Commented Aug 25, 2017 at 11:46
  • 1
    this.age needs to return something, isn't that right? Commented Aug 25, 2017 at 11:48

3 Answers 3

5

Create the object only after the button is clicked:

var Employee = function(name, bd) {
    this.name = name;
    this.bd = bd;
    this.age = function() {
        2017 - this.bd;
    }
}

window.onload = function () {
    document.getElementById('add').addEventListener('click', function(){
        var empName = document.getElementById("name").value;
        var empBday = document.getElementById("bday").value;

        var empList = new Employee(empName, empBday);
        console.log(empList);
    });
}
Sign up to request clarification or add additional context in comments.

Comments

4

You're assigning values to the object in window onload. So empty values are assigned because your input elements have empty values in default

Change it to

window.onload = function () {


var Btn = document.getElementById('add').addEventListener('click', function(){
    var Employee = function(name, bd){
    this.name = name;
    this.bd = bd;
    this.age = function(){
        2017 - this.bd;
    }
}

var empName = document.getElementById("name").value;
var empBday = document.getElementById("bday").value;

var empList = new Employee(empName, empBday);
console.log(empList);
});
}
<html>
    <head>
        <title>Employee Details</title>
        <script src="script.js"></script>
    </head>
    <body>
        <input type="text" id="name" value=""></input>
        <input type="text" id="bday" value=""></input>
        <button id="add">Add</button>
    </body>
</html>

Comments

-1

You need to add handler change event like this :

window.onchange = function () {

var Employee = function(name, bd){
    this.name = name.value;
    this.bd = bd.value;
    this.age = function(){
        2017 - this.bd;
    }
}

var empName = document.getElementById("name")
var empBday = document.getElementById("bday")

var empList = new Employee(empName, empBday);

var Btn = document.getElementById('add').addEventListener('click', function(){
    console.log(empList);
});
}
<html>
    <head>
        <title>Employee Details</title>
        <script src="script.js"></script>
    </head>
    <body>
        <input type="text" id="name"></input>
        <input type="text" id="bday"></input>
        <button id="add">Add</button>
    </body>
</html>

1 Comment

windows.onchange is way too global and will go ham on your browser as it's triggered for many reasons. Worst it multiplies the click bindings in your example duplicating all output the more you "change". If all you need is values when clicking on an element and then localize it to that. Always try to use the most specific not most global selector.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.