ABCaesar
ABCaesar

Reputation: 122

JQUERY - Specific values from same class elements

EDIT: I was really tired last night, and wrote my jQuery out wrong, ".readMore" is always the trigger for it's respective div's animation. I apologize.

Hello Everyone,

I would like to extract specific information from elements with the same class. I have multiple divs that contain varying amounts of text, thus, their heights vary as well. However, I have set the class associated with these divs to a height of 200px. At the bottom of each div, I have an absolutely positioned anchor, that allows the reader to expand(animate) the div to it's automatic/natural height.

What I'd like to do is to find out the 'auto' height of the divs with the same class, store it, and then call on it later when a respective div's anchor is clicked. So far, I keep only getting one value or no value, whereas I'd like all of them.

I've been using .each() recently, but to no avail. The real problem seems to be animating the divs, otherwise, it's no problem; just need to use .css() to set the height to auto. This would be a whole lot easier if you could animate your height to auto. I hope I'm explaining myself correctly. In any case, here's my code.

HTML

<div class="stretch">
    text
    <a class="readMore">Read More</a>
</div>

<div class="stretch">
    text
    <a class="readMore">Read More</a>
</div>

<div class="stretch">
    text
    <a class="readMore">Read More</a>
</div>

CSS

div.stretch {height:auto; width:100%; overflow:hidden; position:relative}
a.readMore {position:absolute; left:0; bottom:0;}

JQuery

$divheight = //stored height of divs
$allheight = $divheight + 100; //extra space to clear the anchor
$(".h200").height(200);
$(".stretch").addClass("h200"); //change height later, to be able to refer to automatic/natural height

$(".readmore").toggle(
    function(){
        $(this).parent('div').animate({height:$allheight}, 750);
        $(".readMore").text("Read Less");
    },
    function(){
        $(this).parent('div').animate({height:"200px"}, 250);
        $(".readMore").text("Read More");
    }
)

Thanks it advance, I really appreciate you even taking a look.

Upvotes: 0

Views: 365

Answers (2)

fx_
fx_

Reputation: 1932

$.height() will return the actual height of your element. Store it, re-use it when calling hide.

Like so:

$(".stretch").toggle(
    function(){
        $(this).data('oh', $(this).height());
        $(this).animate({height: 200}, 750);
    },
    function(){
        $(this).animate({height: $(this).data('oh')}, 250);
        $(this).data('oh', null);
    }
)

Upvotes: 0

Vaibhav Gupta
Vaibhav Gupta

Reputation: 1612

try this:

$hiddenheight=200;
$extraspace=100;

$(".stretch")
.each(function(){
    $(this).data('actualheight', $(this).outerHeight()+$extraspace).css('height', $hiddenheight+'px');    //outerHeight for including padding+margin+border, if any
})
.toggle(
    function(){
        $(this).animate({height: $(this).data('actualheight')+'px'}, 750);
        $(".readMore").text("Read Less");
    },
    function(){
        $(this).parent('div').animate({height:$hiddenheight+"px"}, 250);
        $(".readMore").text("Read More");
    }
)

adjust the height values according to needs..

Upvotes: 1

Related Questions