What is the best way to ensure that only three elements are always visible, even after adding a new one?

I want to ensure that only a maximum of 3 elements are visible on the screen at all times. If one element is removed, the next in line should take its place until there are no more elements left.

Take a look at this code snippet:

<div class="mob-keys-container" id="mob-keys-container">
        <img class="answerIcon mobIcon active" draggable="true" id="answer_1_mob" src="./images/5.1.png" alt="key">
        <img class="answerIcon mobIcon active" draggable="true" id="answer_2_mob" src="./images/5.2.png" alt="key">
        <img class="answerIcon mobIcon active" draggable="true" id="answer_3_mob" src="./images/5.3.png" alt="key">
        <img class="answerIcon mobIcon active" draggable="true" id="answer_4_mob" src="./images/5.4.png" alt="key">
        <img class="answerIcon mobIcon active" draggable="true" id="answer_5_mob" src="./images/5.5.png" alt="key">
        <img class="answerIcon mobIcon active" draggable="true" id="answer_6_mob" src="./images/5.6.png" alt="key">

 </div>

Below is the code that seems to work best for this situation:

$('.info-mob').on('click', () => {
        $('#mob-keys-container').toggle();
        $('#mob-keys-container').children().slice(0, 3).show();
    });

The '.info-mob' function reveals a tab containing all the elements while ensuring that only a maximum of 3 elements are displayed at any time. Although it works well when reopening the tab, I need it to be more dynamic and consistently display a maximum of 3 elements even if new ones are added with a different function.

Answer №1

In my opinion, employing CSS can effectively resolve the problem you are facing.

#mob-keys-container *{
  visibility:hidden;
}

#mob-keys-container *:nth-child(-n+3){
  visibility:visible;
}

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

White background with a black border and transparency on an Android device

I'm trying to create a shape that has a white background with a black border. <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <item> <shape android:shap ...

Invoke Office script from beyond MS Excel WebApp

Within the Excel WebApp on Office 365, users have the ability to incorporate Office Scripts through the "Automate" tab. These scripts utilize JavaScript syntax and can automate Excel functions similar to a VBA macro, specifically designed for the Excel Web ...

The resolveMX function in Google Cloud Functions is encountering issues when trying to process a list of domains

Here is the task at hand. I have a large list of domains, over 100,000 in total, and I need to iterate through them using a foreach loop to resolve MX records for each domain. Once resolved, I then save the MX records into another database. Below is the c ...

Choosing the right framework for a web application

Currently, I am at a crossroads when it comes to deciding on the architecture of the web application I will be developing. As part of a small team, I am tasked with working on this project solo while my colleagues focus on other tasks. The front-end of th ...

Ensure to verify the values of two variables when using a switch case statement

I'm working with two variables that can return true or false. My goal is to display a corresponding text message for each variable if it returns false. How can I effectively handle the ValidCheked and repeatChecked variables in a switch statement? ...

Establishing connections between check boxes and text entry fields and dynamically updating the data within them

I am attempting to connect a checkbox with a text input so that the value inside the input field can be updated. The issue I'm facing is how to appropriately update the value when the checkbox is checked. For instance, my goal is to display the status ...

enhancing look of external tool

I have a third-party widget with inline CSS that displays a table on my website. Is there a way to strip out the inline CSS from the widget before adding it to my page so that only my custom css file styles are applied? The HTML structure of the widget i ...

Node JS Axios Network Error due to CORS Policy Restrictions

When attempting to make a put axios request, I encounter the following error: https://i.sstatic.net/aBQGI.png I have installed and enabled the CORS module in the server.js file, but it doesn't seem to be working. Additionally, there are no CORS head ...

The loop does not seem to be cycling through every item in the JSON dataset

In the JSON dataset provided, there seems to be an issue with the loop only iterating through the first pokemon, Bulbasaur. When typing in names of other pokemons such as "Ivysaur" or "Venusaur", it simply shows "Not found". Take a look at the code snipp ...

Leveraging JQuery for Inserting Data Into mySQL Database

As I delve into JQuery for the first time while working on a project, I realize that this endeavor serves as more of a learning experience rather than a functional one. Currently, I have three PHP files in play: 'connection.php', which establishe ...

Unable to communicate with the server-side controller using Angular

Encountering issues when attempting to call a server-side controller using AngularJS, these two error messages are being displayed. The parameters dictionary contains a null entry for parameter 'problemId' of non-nullable type 'System.Guid& ...

The issue of Google fonts failing to load on Windows browsers while loading perfectly on Mac browsers has been observed

Stackers. I am currently the Front End developer for www.PinionLMS.com. Our website is using "Lato," a Google font. The issue we are experiencing is that when you access the site from a Windows (8.1) browser such as Chrome, Firefox, or Internet Explorer ...

The response from a GET request contains data, however, attempting to access the .data property specifically returns undefined

Sorry if my terminology isn't quite right, I'm still new to fullstack development. Hello there! Currently, I am attempting to retrieve all users from my database. Upon executing the get() request, the client is receiving an OK response as shown ...

Using a for loop in JavaScript to dynamically generate HTML content within a Django project

Do you have a unique question to ask? Version: Django version 3.0.8 This block contains my JavaScript code: fetch(`/loadbox/${message.id}`) .then(response => response.json()) .then(dialog => { let detailcontent= `<div class=" ...

The process of transferring information from JSON to Java

I need help converting the start date from a JSON object to Java in my JSP page. The AJAX call is returning the date as 153452636268, but I want it in a different format. Can someone please provide assistance? $.ajax({ type: "GET", url: ...

Activate the react-select autocomplete feature once a certain number of keystrokes have been input

Is there a method to activate autocomplete only after typing three keystrokes? I am employing the react-select library for this purpose. ...

Node: Sending JSON Values in a POST Request

I am currently working with the index.js file below: var Lob = require('lob')('test_6afa806011ecd05b39535093f7e57757695'); var residence = require('./addresses.json'); console.log(residence.residence.length); for (i = 0; i ...

Is there a way to surround my cursor with a <div> box when clicked?

Looking to replicate the tagging feature on Facebook, I want an empty box to appear when a specific area of an image is clicked. With jQuery, what code can be used to make this box follow the cursor upon clicking? Appreciate any guidance. Thank you! ...

What could be causing the issue of the title in req.body to display as 'undefined'?

I am currently learning about NODE JS and practicing working with POST forms from PUG to a NODE JS server. I am facing an issue where the submission input is coming back as 'undefined' when I submit a form from the web browser. In the code snipp ...

Best practices for isolating a code snippet to run in the background

In my bullCollections, I store various information about messages as an example. try { let bullPayload = { type: 'message', payload: { messsages: messagesForPreProcessingData, sessionID: this.data[&apos ...