javascript intro - object literal
by johnpapa
HTML
<div>
<input text="John" id="nameTxt"/>
<button id="btn">Change it</button>
</div>
<p>Name: <span id="msg"></span></p>
JavaScript
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();
});