What is the best way to display two items from a list while hiding the rest?

I received a list generated from the backend that looks like this:

<ul>
{% for item in items %}
  <li>{{item }}</li>
 {% endfor %}
</ul>

My goal is to display only the first two items and collapse the rest, possibly with a 'see all' link that reveals the remaining items when clicked. Is this achievable?

Answer №1

$(function(){
var limit = 1;

     $(".list-group-item").each(function(i) {
        if (i <= limit) {
          $(this).addClass('show');
        }else{
        $(this).addClass('add-more');
        }
        $(this).not('.show').hide();
      });

})

  $('.see-emails').click(function() {
  $('.add-more').show();
    $('a').hide();
  });
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>


<div id="collapseListGroup1">
  <ul class="list-group">

    <li class="list-group-item"> email1 </li>
    <li class="list-group-item"> email2 </li>
    <li class="list-group-item"> email3 </li>
    <li class="list-group-item"> email4 </li>
    <li class="list-group-item"> email 5</li>
  </ul>
</div>
<a class="collapsed see-emails" >
          See Emails
        </a>

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

The function inside setTimeout lost its scope

I'm struggling to make the following function work correctly. All aspects seem fine except that I believe I am encountering scope issues within the setTimeout function. This is the JavaScript code: function galContent(){ var hovInt; $(".eleme ...

If the span id includes PHP data that contains a certain phrase

Hey there, it's my first time posting and I'm in a bit of a bind with this script... Let me give you some background information first I am trying to create a click function for a register button that will check the span id (e.g. $("#username_r ...

Tips for incorporating captions into a slideshow

I've been experimenting with different methods to include captions in my slideshow, but haven't had much success. I simply want to add a div at the bottom of each image. Do I need to modify my script or should I consider adding something else? H ...

display the JSON response using jQuery

Having an issue with printing the response JSON using a message alert. Here is the script I am using: $.ajax({ headers: { "Accept" : "application/json", "Content-Type": "application/json" }, url: "http:/ ...

Resolving the Enigma: Querying jQuery for Real-Time Validation and

I'm fairly new to jQuery and I'm facing a challenge in my registration form script. Specifically, I want to check if the entered username or email is already taken while the user is typing. Currently, this functionality works by making a json req ...

Guide on how to use Ajax in Flask to render a template after a post request

I'm looking to dynamically render a new template after making a jQuery ajax post request. Can someone provide guidance on how to achieve this using jQuery/ajax? Below is the initial route where the post request is sent: @app.route("/data") def data( ...

The function 'toLowerCase' cannot be found for the type 'string | number | string[]'. Similarly, the function 'toLowerCase' cannot be found for the type 'number'

Currently, I am working on a Laravel project using Laravel Mix. I am attempting to create a table with filter functionality. However, when I insert the following code into my TS file: import $ from 'jquery'; import 'bootstrap'; $(() = ...

Sending AJAX information to multiple pages

I have created an HTML page where I am struggling to pass two variables using the POST method to a PHP page. The PHP page is supposed to accept these variables and then call an API to retrieve data based on them. However, my challenge is in receiving this ...

Transparent CSS Table Styling

Can you create a CSS effect where all content inside a table becomes transparent on hover? For example: <table><tr><td> AN IMAGE </td> <td> SOME TEXT </td></tr></table> So if either the image or the text ar ...

Update the second dropdown menu dynamically according to the choice made in the first dropdown list

I need help with creating two drop down lists, ddl1 and ddl2. When an item is selected in ddl1, I want the corresponding items to be populated in ddl2. For example, if "Accounts" is selected in ddl1, then ddl2 should display "Account officer, Account man ...

Is it possible that an object sent as a response body from a Spring MVC application's controller cannot be fetched using the jQuery AJAX method?

After making an AJAX call, the EmployeeBean object is not being returned and no exception is being thrown. Here is the code snippet from the controller method: I am trying to return an EmployeeBean object from this method using @Responsebody @RequestMapp ...

Modifying an HTML attribute dynamically in D3 by evaluating its existing value

I'm facing a seemingly simple task, but I can't quite crack it. My web page showcases multiple bar graphs, and I'm aiming to create a button that reveals or conceals certain bars. Specifically, when toggled, I want half of the bars to vanish ...

Tips for accessing a local file on a website through HTML - easy steps to follow!

Is it possible to open a local (jpg or pdf) file from a random website that is not under my control? I have tried editing the HTML code of a site in Chrome by replacing existing links with ones linking to my local file, but when I click on them, nothing ha ...

Created a custom function that includes a specific target href parameter

I have a website that implements role-based authentication, where the application displayed is dependent on the user's role. The code for this functionality is as follows: <asp:Repeater ID="ui_rprApp" runat="server" DataSourceID="odsApp"> ...

What is the most effective way to extract data from HTML that contains mathematical notations and convert it into plain text format?

Using BeautifulSoup to extract text from HTML can be a bit tricky, especially when dealing with math tags. Below is the HTML code snippet that uses math tags to display text: <p> <span class="aps-inline-formula"> <math display="inline" x ...

Creating Canvas dimensions to match the dimensions of a rectangle specified by the user

When the code below runs, it correctly generates a rectangle the same size as the canvas on start-up. However, an issue arises when the user clicks the button to generate a new canvas - the rectangle does not appear. Can someone please provide assistance ...

Maintain the fixed position of the Google Map <div> while allowing background <div>s to scroll

I'm having trouble keeping a Google Map div fixed on the screen while allowing my background, consisting of multiple scrolling divs with content inside, to scroll as normal. My attempt to wrap the map div in another fixed div resulted in the map disa ...

Click outside of this popup to dismiss it

I have been struggling to figure out how to make this popup close when clicking outside of it. Currently, it only closes when clicked directly on it. Your assistance in this matter is greatly appreciated. Here is the HTML code: <h2>Popup</h2> ...

Instructions on displaying the main page even with just inputting the default directory in the URL

One scenario is when I input file:///C:/xampp/htdocs/At/html/ in the URL. My desired outcome is for file:///C:/xampp/htdocs/At/html/pages-login.php to be displayed instead. How can I achieve this? ...

Tooltips envelope around themselves

Several paragraphs contain tooltips, but some of them break onto a new line instead of staying within the text. <div class="row"> ... </div> Fiddle: https://jsfiddle.net/LhjmLw8k/29/ The issue is that certain words like "Trasimeno" and ...