Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Jan Reefs
Jan Reefs
11,161 Points

Contact database jquery

Hi all,

I am trying to find a way to store the variables first name, surname, address and phoneNumber in a contact database (object with properties). How can I make sure they are stored correctly in a to be created "database" so that I can call/find the contact details later on? Is this with properties?

Thanks!


$(document).ready(function() {

console.log('hello')

$('.newContact').on('click', function(event) { event.preventDefault(); console.log('You clicked me');

var $firstName = $('.firstName'); var firstName = $firstName.val(); console.log(firstName);

var $surname = $('.surname'); var surname = $surname.val(); console.log(surname);

var $phoneNumber = $('.phoneNumber'); var phoneNumber = $phoneNumber.val(); console.log(phoneNumber);

var $address = $('.address'); var address = $address.val(); console.log(address);

})

})

3 Answers

Steven Parker
Steven Parker
231,169 Points

Object properties could be a good way to model a database, but be careful not to confuse HTML element properties with JS object properties.

Also, when showing code, remember that the HTML part can be as important to the functionality as the JavaScript.

Jan Reefs
Jan Reefs
11,161 Points

Could you please explain me how I can store the variables in object properties?

Steven Parker
Steven Parker
231,169 Points
var user = {};      // start with empty object
user.firstName = $('.firstName').val();
user.surname = $('.surname').val();
user.phoneNumber = $('.phoneNumber').val();
user.address = $('.address').val();

console.log(user);  // show the filled object
Jan Reefs
Jan Reefs
11,161 Points

ok thank you!

So I am now able to store all inputs into this user object (with properties firstname,surname,phonenumber and address).

How can I now create a searchable database where I can add multiple users and search on these users?

Steven Parker
Steven Parker
231,169 Points

Charle Van den Berghes — I saw you created another question, you can see my answer there.

Jan Reefs
Jan Reefs
11,161 Points

Ok I marked it. Could you please help me with my question?