HTML:
<div class="nav">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</div>
Jquery:
$('.nav ul li a').click(function () {
var url = $(this).attr('href');
var obj = {};
$('.cnt').load(url + ' div', function (response) {
obj['key'] = $(this).html();
});
return false;
});
My problem is that every time I click, the obj only gets the key once. I want on every next click the object to hold the previous key and add the new one. It should look something like this:
1st click obj{key}
2nd click obj{key,key}
now it looks like this:
1st click obj{key}
2nd click obj{key}
An object can only have one value per key. You're setting the 'key'
value of obj
, so it's re-setting that key each time.
You want to be using an array instead.
var arr = [];
$('.nav ul li a').click(function () {
var url = $(this).attr('href');
$('.cnt').load(url + ' div', function (response) {
arr.push($(this).html());
});
return false;
});