I have elements in a
and I want them expand down and up and each element should expand down and up.
HTML:
Target the clicked elements parent instead of all the .answers
$(document).ready(function(){
$('.expand').click(function(){
$(this).parent().css('height','auto');
})
});
http://jsfiddle.net/Tmm6R/ fiddle
To toggle the others, target the siblings and set them to default
$(document).ready(function(){
$('.expand').click(function(){
$(this).parent().css('height','auto');
$(this).parent().siblings().css("height", "20px");
})
});
http://jsfiddle.net/Tmm6R/1/
If you also want to the opened one to be togglabble, you could do something like this:
$(document).ready(function(){
$('.expand').click(function(){
var parent = $(this).parent();
console.log(parent.css("height"));
if(parent.css("height") > "20px"){
parent.css("height", "20px");
} else {
parent.css("height", "auto");
}
$(this).parent().siblings().css("height", "20px");
})
});
http://jsfiddle.net/TCxP5/