A feature to reveal additional content when it extends beyond a single line

<div class="questionnaire-description col-xs-12" id="questionnaire_description_wrapper">
                <text-truncate>
                    <span ng-class="questionnaire_description.show_more ? 'full-description' : 'ph-ellipsis'" id="questionnaire_description">
                       <span class="ph-caption" ng-bind="questionnaire_details.description"></span>
                    </span>
                </text-truncate>
                <span class="text-center questionnaire-sub-details" ng-if="!questionnaire_description.show_more">
                <span class="ph-caption cursor-pointer" ng-bind="'SEARCH_SHOW_MORE'|translate" ng-click="questionnaire_description.show_more=true" ng-if="!questionnaire_description.show_more"></span>
                </span>
            </div>
The show more button with ellipsis will only appear if the text is long enough to require it to be on more than one line. If the text fits into just one line, no ellipsis or show more button will be displayed. In cases where the text is lengthy and requires ellipsis, the show more option will be placed at the end of the first line for user convenience.

https://i.sstatic.net/EMi2R.png

Answer №1

To achieve this effect, a combination of a little JavaScript and the CSS property text-overflow can be used.

By applying white-space: nowrap, the text will remain on a single line without wrapping onto multiple lines.

Setting the width to a fixed 200px and using overflow: hidden will truncate the text that exceeds the specified width.

Adding text-overflow: ellipsis will display three dots at the end of the truncated text.

The "Show more"/"Show less" buttons simply toggle between displaying the full text and the truncated version.

var $text = jQuery(".text");
var $button = jQuery(".show-more");

$button.on("click", function(){
    $text.toggleClass("is-expanded");

    if($text.hasClass("is-expanded")){
        $button.text("Show less");
    } else {
        $button.text("Show more");
    }
});
.text {
    width: 200px;
    text-overflow: ellipsis;
    white-space: nowrap;
    overflow: hidden;
}
.text.is-expanded {
    white-space: initial;
    overflow: visible;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="text">
This is very very long text and it is hidden at the end.
This is very very long text and it is hidden at the end. This is very very long text and it is hidden at the end.
This is very very long text and it is hidden at the end.
</div>
<button class="show-more">Show more</button>

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

How to customize a disabled paper-input element in Polymer 1.0?

Help with Polymer 1.0: I've encountered an issue where setting a paper-input element to 'disabled' results in very light gray text and underline, making it hard to read. I've been trying to use CSS to change the text color but haven&ap ...

Ensuring Data Accuracy in Angular 2 Forms

Currently, I am working on form validation using Angular 2 and encountered an issue with the error message Cannot read property 'valid' of undefined. The HTML file I am working on contains a form structure like this: <form id="commentform" c ...

Calculating the quantity of elements within a jQuery array

A JQuery array is proving to be quite problematic. Here's what it looks like, [125, "321", "kar", 125, "sho", "sho", 12, 125, "32", 32] Unfortunately, there are duplicates present in this array. My goal is to obtain the count of each unique element ...

How can I generate an AJAX object to handle multiple requests or as an array?

I am looking to dynamically create Ajax objects for each request and execute a function for each object using jQuery. Here is an example in JavaScript: xhr[rand] = new XMLHttpRequest(); xhr[rand].open("post", "base64upload", true); xhr[rand].upload.add ...

What is the best way to ensure that JavaScript is loaded in a specific sequential order?

I'm having trouble ensuring that JS files load in the correct sequence, despite my efforts to research async and defer on various platforms. This is the structure of my code: <script src="lang.js"></script> <!--loading either ...

I'm attempting to retrieve a JSON response with Jquery-Ajax, but I'm running into issues. The selection dropdown is loading without any options

Upon hitting http://localhost:8082/getCountries, the SOAPUI (GET) response is as follows: HTTP/1.1 200 Content-Type=application/json Transfer-Encoding=chunked Date=Mon, 18 May 2020 03:38:46 GMT Keep-Alive=timeout=60 Connection=keep-alive [{"countryI ...

Carousel Alert: Ensure that every child within the list is assigned a distinct "key" property

I'm experiencing an issue that is proving to be more challenging than usual, as it appears to be related to a specific library. I understand that using a key from a file is not ideal, but the plan is for it to come from a database in the future. Libr ...

`Python automation for seamless HTTP browsing and HTML document generation`

My weekly routine at work involves printing out Account analysis and Account Positions for more than 50 accounts every Monday. I have to navigate through the page, select "account analysis", input the account name, format the page for printing, print the o ...

The $http.get in AngularJS is causing a problem by returning undefined and $http() is not being recognized

I am currently working on an app that is designed to load and display data dynamically from a database using AngularJS. However, I have been encountering errors when trying to access my API using both $http() and $http.get(). The specific errors I am recei ...

The front end button stubbornly refuses to comply and redirect to another page

I am having trouble getting my button element to redirect my page to another page. I have tried using an onclick function inside the button tag with window.location.href, as well as creating a separate function for the redirect, but nothing seems to be wor ...

Transferring image data to a different webpage

Currently, I am facing an issue with obtaining image data from a camera and photo album on a mobile device. Although I have successfully retrieved the chosen image using the provided code snippet below, my dilemma lies in transferring this image data to an ...

Instructions for incorporating highcharts sub modules into a React application

I have been utilizing the react-jsx-highcharts library to seamlessly integrate Highcharts into my React application. Everything is functioning perfectly. However, I am now interested in incorporating the boost module. I attempted to add it by simply using ...

Use `$$state: {…}` within the request rather than the data

When attempting to send a request with data, I am only getting "f {$$state: {…}}" in the console. $scope.createTask = function () { var req = $http.post('api/insert', { title: $scope.newTitle, description: ...

Adjust positioning of navigation when hovered over

Need help creating a cool navigation effect like this. Live example: https://hookandbarrelrestaurant.com/ Here is my code: https://codepen.io/Dhaval182/pen/rQPMoW ...

``How can I extract the last string in Vue.js when working with string processing?

I've been working with strings in vuejs and currently have 4 URL strings: https://web-sand.com/product/slug/apple-iphone-13 https://web-sand.com/product/slug/samsung-galaxy https://web-sand.com/product/slug/xiaomi-red https://web-sand.com/product/slug ...

Issue encountered with Express.js and connect-mongo session: "TypeError - Unable to access property 'upserted' as it is undefined"

I'm working on implementing session storage using the connect-mongo module, but I keep encountering the following error: TypeError: Cannot read property 'upserted' of undefined Here is how I'm using the connect-mongo: import session ...

Cloud function -> time stamps evaluation

I've been working on a cloud function to delete items in the "links" collection that have an end time older than the current timestamp. Although my function runs without any errors, it's not deleting the items as expected and is causing me quite ...

Link the Sass variable to Vue props

When working on creating reusable components in Vue.js, I often utilize Sass variables for maintaining consistency in colors, sizes, and other styles. However, I recently encountered an issue with passing Sass variables using props in Vue.js. If I directly ...

Toggling multiple sets of checkboxes using Jquery

Currently, everything is functioning as expected, but there seems to be a minor issue... In my setup, I have 2 sets of checkbox blocks labeled Group A and Group B If I choose 'all' for Group A, all checkboxes in that group toggle their state ac ...

What is the best way to efficiently transmit Objects through AJAX utilizing bodyParser in node.js express?

Currently attempting to execute: $.ajax({ type:"POST", url:"/psychos", data:JSON.stringify(this.psycho) }) Upon reaching the server, I encounter the following: app.post("/psychos", function(request, respon ...