Expand the box using jQuery to display overflowing content

My task involves loading multiple lines of text in each div box within a category listing. To display only three lines upon loading, I have limited the height and added overflow:hidden; CSS. A small button (displaying image loaded in div) positioned next to each box is meant for adjusting the height accordingly. However, this feature seems to be malfunctioning. Below are the relevant code snippets:

HTML (one of the boxes on the page):

<div class="content" style="height:50px;overflow:hidden;">
    <div>Category 1</div>
    <div>Category 2</div>
    <div>Category 3</div>
    <div>Category 4</div>
    <div>Category 5</div>
    <div>Category 6</div>
</div>
<div class="see-more"></div>

jQuery:

jQuery(document).ready(function() {
    jQuery(".see-more").click(function() {
        jQuery(this).prev("div.content").height(200);
    });
});

The jQuery code appears to be encountering issues. Additionally, I am seeking guidance on determining the actual height of hidden content (due to overflow) in order to set an accurate height value for the box rather than using a static 200.

Answer №1

To determine the maximum height of items, you can enclose the inner items and calculate the height of that container. You may also increment the height in incremental steps to set the full height.

$(function(){
    $('.content').wrapInner('<div class="inner">');  

    $(".see-more").click(function() {
        var $cont=$(this).prev()
        var maxHeight = $cont.find('.inner').height();
        $cont.height(maxHeight);
    });
 });

Check out the DEMO: http://jsfiddle.net/nq2Em/1/

Answer №2

To accurately determine the height of a container, you can utilize the scrollHeight property. If you prefer not to incorporate any animations, another option is to eliminate the overflow hidden style. Take a look at this example for a demonstration of the height solution:

When utilizing jQuery's document ready function along with a click event on an element with a class of "see-more", you can set the height of a content box based on its scrollHeight property:

jQuery(document).ready(function() {
    jQuery(".see-more").click(function() {
        var box = jQuery(this).prev("div.content"),
            height = box[0].scrollHeight;
        box.height(height);
    });
});​

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What could be causing certain Bootstrap classes and CSS properties to not function properly in my code?

My goal was to create a website template using BOOTSTRAP and CSS. However, I encountered some issues in my code where certain Bootstrap classes are not functioning as expected, such as lead, font-weight, color, bg-color, etc. I noticed that if I apply a Bo ...

Adding elements to a JavaScript modal using a loop

When working with JavaScript, I am dynamically adding content to a modal. This involves populating different data into various table rows and cells within the modal. <table class="table table-striped"> <thead> <tr> ...

Obtaining a Collection<Item> through JQuery and JSON: A Step-by-Step

Currently, I am immersed in a java web project that involves utilizing tomcat, jsp, bootstrap, struts2, mysql, and gson. Within this project, there exists a significant entity known as Product: public class Product{ private long id; private Strin ...

Maintaining an even distribution of flex children on each line as they wrap to the next line

I am curious about finding a way to maintain an even distribution of elements on each line, including the last one, using flex-wrap or any other flexbox technique. For example, let's say I have a flexbox with 6 elements. When it wraps, I would like t ...

Is there a way to transform BASE64 encoded HTML into a GIF format using ColdFusion?

I am getting a BASE64 encoded message from a WebService. This message is an interpretation of an HTML page and I can use pre-existing ColdFusion functions to convert and showcase it. However, my requirement now is to generate a GIF image of the HTML conten ...

Tips for adding and removing active class with navbar links using JavaScriptonclick

I need help with toggling the "li-active" class on my navigation bar links. Currently, when I click on a link, the "li-active" class is transferred from the previous link to the newly clicked link instead of removing it first. Can someone please assist me ...

What causes an ASP.net page to appear as plain text instead of rendering properly?

Can someone help me with the issue I'm facing? [ Server.Transfer(url) ] I have a function in my code that is supposed to redirect to an html page, But for some reason, the html page is displaying as text in the browser. Why is this happening? Th ...

Upon loading the page, the Font Awesome icon initially appears in full screen before adjusting to the proper size

Struggling to locate a resolution on the platform. Utilizing a font awesome icon with the react library, I encounter an issue where the icon renders in full screen upon page load before resizing to its intended size. I have attempted setting the size prope ...

Adding identifiers to columns in DataTables depending on the value of another attribute

I am facing a challenge with inserting the <span> tag into the "salesStatusName" column cell that already contains some data. This should only happen when the value of the last column, "completelyDelivered", is true. However, I do not want to display ...

Using getJSON in conjunction with jQuery's vTicker for dynamic content scrolling

I have come across some code to retrieve JSON data from another website, similar to the following: <html> <head><script src="//code.jquery.com/jquery-1.10.2.js"></script> <script src="http://code.jquery.com/jquery-migrate-1.2.1. ...

Split into two lines, the Bootstrap carousel includes 28 indicators for easy navigation

I am currently using a Bootstrap 4 carousel with 28 pictures. However, I am facing an issue where the indicators on small and medium devices are not displaying properly (some indicators seem to be missing). They are not breaking into new lines as expected. ...

Div positioning calculation malfunctioning

How can I position the divs as described below? I want the left div to take up all available space except for 400 px which is designated for the div resource. Currently, the two divs are overlapping. If I set the width of both divs to 400px, there is a l ...

What is the best way to adjust the height of an image in a Bootstrap 4 column to ensure that it remains responsive without affecting the size of an embedded video in a neighboring column?

Looking for a layout with an image, embedded video, and text all in one row? The responsive website relies on Bootstrap but needs the items in this row to maintain their order without stacking when the page width is decreased. The issue arises when an ima ...

`ASP.NET utilized for displaying several pictures on a website`

My goal is to retrieve images from a database and showcase them on a webpage. I am looking to incorporate a "show more images" button in case there are too many images to display at once. Additionally, I would like for the additional images to load autom ...

Achieving the retrieval of all data can be done simply by utilizing the `echo json_encode($data);

I am trying to automatically update the user wall with new data from the database using a script that checks for updates every 15 seconds. Everything works fine when I only use one piece of data like $data = $row['description']; in the server.ph ...

How to delete an element from a session array using Jquery and Ajax techniques

In my table, each element in an array corresponds to a row. I'm attempting to delete an element when the delete image (with the id deleteRowButton) is clicked. Currently, nothing happens upon clicking the image. However, if I remove the line var index ...

Unfinished Handling of Callbacks in jQuery POST Requests

When it comes to handling AJAX posts in JavaScript, I have encountered two methods that work differently in terms of page refreshing and element display. 1) The Delete method removes an element upon callback without refreshing the page: function DeleteRu ...

Obtain Data from a Table by Clicking a Button with jQuery

In the code snippet below, a table displays date time strings and a Close button. Upon clicking the close button, the respective row is removed; however, before removal, I would like to store the date in a variable. $(document).on('click', &ap ...

The animated sticky header lacks smooth animation while scrolling upwards

This dynamic menu transitions the logo to the left and the navigation to the right as you scroll down. The animation is smooth when scrolling down, but slightly jerky when scrolling up, especially with the navigation. The animation uses CSS3: transition: ...

Find out if the On/Off switch is in the "on" or "off

My HTML switch code is shown below: $('#myonoffswitch').click(function () { alert($('#myonoffswitch').val()); }); .onoffswitch { position: relative; width: 77px; -webkit-user-select ...