Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9296309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T21:48:14+00:00 2026-06-18T21:48:14+00:00

I have three div’s and I have function that shows full content of them

  • 0

I have three div’s and I have function that shows full content of them when I click on button.
But I think that function is too complex, I want something without so many if statements.

If I had 10 div’s this function would be too long, how to make it shorter?

$(document).ready(function () {
var h1 = $(".pt1").height(), h2 = $(".pt2").height(), h3 = $(".pt3").height();
$(".post-text").height("72px");
var read = $(".post-read");

read.click(function () {
    if ($(this).html() === 'Read more') {
        $(this).html("Show less");
        if ($(this).prev().attr('class') == "post-text pt1") {
            $(this).prev().animate({
                height : h1
            }, 'slow');
        } else if ($(this).prev().attr('class') == "post-text pt2") {
            $(this).prev().animate({
                height : h2
            }, 'slow');
        } else {
            $(this).prev().animate({
                height : h3
            }, 'slow');
        }
    } else {
        $(this).html("Read more");
        $(this).prev().animate({
            height : 72
        }, 'slow');
    }
});
});

HTML:

<div id="tabs-1">
                <div class="post">
                    <div class="post-img">
                        <img alt="" src="img/post-pic.png">
                    </div>
                    <div class="post-text pt1">Pellentesque habitant morbi tristique senectus et netus et malesuada fames
                        Pellentesque habitant morbi tristique senectus et netus et male- suada fames Pellentesque habitant
                        morbi tristique senectus et netus et malesuada fames Pellentesque habitant morbi tristique senectus et
                        netus et male- suada fames Pellentesque habitant morbi tristique senectus et netus et malesuada fames
                        Pellentesque habitant morbi tristique senectus et netus et male- suada fames Pellentesque habitant
                        morbi tristique senectus et netus et malesuada fames Pellentesque habitant morbi tristique senectus et
                        netus et male- suada fames</div>

                    <div class="post-read">Read more</div>
                    <div class="clear"></div>
                </div>
                <div class="post">
                    <div class="post-img">
                        <img alt="" src="img/post-pic.png">
                    </div>
                    <div class="post-text pt2">Pellentesque habitant morbi tristique senectus et netus et malesuada fames
                        Pellentesque habitant morbi tristique senectus et netus et male- suada fames Pellentesque habitant
                        morbi tristique senectus et netus et malesuada fames Pellentesque habitant morbi tristique senectus et
                        netus et male- suada fames Pellentesque habitant morbi tristique senectus et netus et malesuada fames
                        Pellentesque habitant morbi tristique senectus et netus et male- suada fames Pellentesque habitant
                        morbi tristique senectus et netus et malesuada fames Pellentesque habitant morbi tristique senectus et
                        netus et male- suada fames Pellentesque habitant morbi tristique senectus et netus et malesuada fames
                        Pellentesque habitant morbi tristique senectus et netus et male- suada fames</div>
                    <div class="post-read">Read more</div>
                    <div class="clear"></div>
                </div>
                <div class="post">
                    <div class="post-img">
                        <img alt="" src="img/post-pic.png">
                    </div>
                    <div class="post-text pt3">Pellentesque habitant morbi tristique senectus et netus et malesuada fames
                        Pellentesque habitant morbi tristique senectus et netus et male- suada fames Pellentesque habitant
                        morbi tristiquemorbi tristique senectus et netus.</div>
                    <div class="post-read">Read more</div>
                    <div class="clear"></div>
                </div>
            </div>

CSS:

.post {clear: both;width: 595px;padding-bottom: 30px;}
.post-img {width: 122px;background-color: #f8f6f1;box-shadow: 0px 0px 5px 1px #a09e99;float: left; }
.post-text {float: left;width: 457px; overflow:hidden;padding-left: 16px;}
.post-read {float: right;background: url(img/arrow.png) no-repeat right center transparent;padding-right: 22px;margin-right: 12px;}

jsFiddle: link

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T21:48:15+00:00Added an answer on June 18, 2026 at 9:48 pm

    This is the situation when you should write jQuery plugin. This is easy:

    (function ($) {
        $.fn.readMore = function () {
            return this.each(function () {
                var h = $(this).height();
                $(this).height("72px");
                $(this).next().click(function () {
                    if ($(this).html() === 'Read more') {
                        $(this).html("Show less");
                        if ($(this).prev().hasClass('post-text')) {
                            $(this).prev().animate({
                                height: h
                            }, 'slow');
                        }
                    }
                    else {
                        $(this).html("Read more");
                        $(this).prev().animate({
                            height: 72
                        }, 'slow');
                    }
                });
            });
        };
    })(jQuery);
    
    $(function() {
        $('.post-text').readMore();
    });
    

    http://jsfiddle.net/dfsq/S9KEz/1/

    This is a scetch, you should improve it. For example you should not use such a comparison like $(this).html() === 'Read more' – your code will stop working if you change the text.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three divs, within a content div. Container width 70%. Within that I
Say that I have three button elements in one parent div. Assuming that these
I have a simple function that I wrote that transitions three div elements using
I have three div's next to eachother, but when the content of one or
I have three div with dynamic content, and I need that the height of
I have a div that contains an h2 and three div 's, but it
I have a simple function that I wrote that transitions three div elements using
i have three divs like that: <div id=1 >id=1</div> <div id=2>id=2</div> <div id=3>id=3</div> Now
my site has three div elements, header, content and footer. i have a picture
sliding div using javascript i have three div with some content. i need the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.