I’m currently learning jquery and have run into a snag. I’m working on building a page with two sections. One is an unordered list of food types. The other is a set of divs with each displaying different types of beer that would complement each of the food types. Each list-item and its corresponding div share the same class. All of the divs are positioned out of view to start.
What I want to accomplish is that onclick of each list-item, whatever div that is in view animates out of view, and then the corresponding div of the clicked list-item animates into view.
I got this working, but then for some reason, it stopped working. I do not know if this has something to do with my code or if there is a better way to go about accomplishing this project.
$(function(){
$('.beer_container > div.default').css('top', '0');
$('ul.food_items li').click(function(){
var theClass = $(this).attr('class');
var $theBeer = $('div.beer_container > div[class="+ theClass +"]');
var $oldBeer = $('div.beer_container > div:not([class="+ theClass +"])');
$oldBeer.animate(
{'top': '-420'},
'slow', function(){
$theBeer.animate(
{'top': '0'}, 'slow');
});
});
});
<div class="left food_container">
<ul class="food_items">
<li class="lighter_foods">Lighter Foods</li>
<li class="classic_burger">Classic Burger</li>
<li class="strong_spicy">Strong & Spicy</li>
<li class="rich_hearty">Rich & Hearty</li>
<li class="smoked">Smoked Meats</li>
</ul>
</div>
<div class="right beer_container">
<div class="default">
This is the intro page. What are you eating?
</div>
<div class="lighter_foods">
Blonde Ale, Hefeweizen, Wheat Ale, Witbier, Pilsener
</div>
<div class="classic_burger">
Pale Ale, Amber/Red Ale, Amber Lager
</div>
<div class="strong_spicy">
IPA, Amber/Red Ale, Abbey Tripel, Sweet or Oatmeal Stout, Oktoberfest/Maerzen, Pale Bock
</div>
<div class="rich_hearty">
Bitter, Scotch Ale, Brown Ale, Abbey Dubbel, Old or Strong Ale, Porter, Dry Stout, Dunkelweizen, Weizenbock, Doppelbock, Dark Lager
</div>
<div class="smoked">
Double/Imperial IPA, Brown Ale, Abbey Dubbel, Porter, Imperial Stout
</div>
</div>
Looks like some single quotes became double quotes.
I’m not endorsing that method of targeting, but you definitely don’t want to be looking for div with the class “+ theClass +”