var person = { firstName: 'John', lastName: 'Papa', changeMyName: function(){ this.firstName = $('#nameTxt').val(); } }; //extend the person with an age property person.age = 21; var displayName = function() { $('#msg').text(person.firstName); }; // display the name displayName(); $('#btn').click(function() { //change the name to Brad person.changeMyName(); displayName(); });
<div> <input text="John" id="nameTxt"/> <button id="btn">Change it</button> </div> <p>Name: <span id="msg"></span></p>