Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery detecting first paragraph height different using .attr() .first()
    text
    copied!<p>I'm trying to get the first paragraph height within a div and store it as an attribute.</p> <p>It works on the first paragraph in the first div with the class <code>.actor-bio</code>.</p> <p>On the second div with the class <code>.actor-bio</code> it displays the attribute of the first div's paragraph.</p> <pre><code> //has to be in an each to get different values of varying paragraph lengths $('.actor-bio').each(function(){ //Gets original height of the content area $(this).attr('originalheight', $(this).innerHeight()); //Gets the first paragraph height $('p', this).first().attr('firstpheight', $('p',this).first().innerHeight()); //Gets the first h3 height $('h3', this).first().attr('firstheaderheight', $('h3',this).first().innerHeight()); //Sets the height of the wrapper to first paragraph height + h3 height $(this).css({height : parseFloat($('p', $(this).closest('.actor-bio-wrapper')).first().attr('firstpheight')) + parseFloat($('h3', $(this).closest('.actor-bio-wrapper')).first().attr('firstheaderheight'))}); }); </code></pre> <p>Has anyone got any suggestions?</p> <p>Thanks in advance</p> <p><em><strong></em>*</strong><em>Edit 16:44GMT 22/06/2012</em><strong><em>*</em>*</strong></p> <p>Here is the jsFiddle link: <a href="http://jsfiddle.net/SqzL5/2" rel="nofollow">http://jsfiddle.net/SqzL5/2</a></p> <p>Annoyingly my code works with jsFiddle, it must get mixed up with another "<code>(this)</code>" elsewhere causing the issue. Using defuz's method also works in jsFiddle, but not on my site grr. <a href="http://jsfiddle.net/SqzL5/1/" rel="nofollow">http://jsfiddle.net/SqzL5/1/</a></p> <p><em><strong></em>*</strong><em>Edit 11:17GMT 26/06/2012</em><strong><em>*</em>*</strong></p> <p>Right I gave up on this method as the code seemed to only be getting 1 paragraph value. Anyway I did this instead.</p> <pre><code> // Read more after first paragraph if($('.read-bio').length&gt;0){ //has to be in an each to get different values of varying paragraph lengths $('.actor-bio').each(function(index, item){ $('p:not(:first)', item).hide(); }); $('.read-bio').click(function(){ //If class has class 'clicked' animate to first p height + h3 height if($(this).hasClass('clicked')){ $('.actor-bio p:not(:first)', $(this).closest('.actor-bio-wrapper')).animate({height: 'toggle'}); $(this).html('&amp;raquo; &lt;strong&gt;Read&lt;/strong&gt; More').removeClass('clicked'); } //If no 'clicked' class, animate the height to the original height of the text else{ $('.actor-bio p:not(:first)', $(this).closest('.actor-bio-wrapper')).animate({height: 'toggle'}); $(this).html('&amp;raquo; &lt;strong&gt;Read&lt;/strong&gt; Less').addClass('clicked'); } //stops the stupid # appearing the address bar return false; }); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload