Display a random div element in jQuery without specifying a specific class

I have a dynamic list that I am displaying in a div as shown below

<div class="cards card1">
  <div class="front">
    <p>Front 1</p>
  </div>
  <div class="back1" style="display: none">
    <p>Back 1</p>
  </div>
</div>

<div class="cards card2" style="display: none">
  <div class="front">
    <p>Front 2</p>
  </div>
  <div class="back2" style="display: none">
    <p>Back 2</p>
  </div>
</div>

<div class="cards card3" style="display: none">
  <div class="front">
    <p>Front 3</p>
  </div>
  <div class="back3" style="display: none">
    <p>Back 3</p>
  </div>
</div>

When the page loads, only the first main div (card1) is displayed. There is a button called "Show". Clicking on this button shows the second sub-div (back1) of the first main div using jQuery. It also adds a class to the main div (card1) called 'visited'.

There is another button named "Next". When clicking on the "Next" button, I want to shuffle the remaining divs (card2, card3) and randomly display one of these two divs. The shuffling should exclude divs with the 'visited' class.

For example, if I click the reveal button for the first div and the 'visited' class is added to it (card1), there are two remaining divs without the 'visited' class. Clicking the "Next" button should show either card2 or card3, not card1 because it has the 'visited' class.

If the card3 div appears and I click on the "Next" button again, it should show the card3 div since it is the only one without the 'visited' class.

I have many such divs. Please assist me with this functionality.

Answer №1

When working with jQuery, you can utilize the `not()` selector to refine your query results. For selecting a random element, you can refer to the technique demonstrated in this helpful answer. Combining these concepts, you can achieve the following:

const unvisitedCards = $('.cards').not('.visited');
const randomIndex = Math.floor(Math.random() * unvisitedCards.length);
const nextCard = unvisitedCards.eq(randomIndex);
nextCard.show();

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

Utilizing Shopify API to seamlessly add items to cart without any redirection for a smoother shopping experience

Looking for a solution to submit an add to cart POST request without any redirection at all? I have tried changing return_to to "back" but that still reloads the page, which is not ideal. My goal is to smoothly add the item to the cart and notify the cli ...

"Created a persistent fullscreen overlay over the body content, complete with scroll bars

Can a fixed fullscreen division position:fixed; width:100%; height:100%; cover the entire body of a page, including scroll bars? I understand that setting the body to overflow:hidden; can achieve this, but I am facing an issue where I want the fullscreen ...

Following an AJAX request, jQuery.each() does not have access to the newly loaded CSS selectors

Note: While I value the opinions of others, I don't believe this issue is a duplicate based on the provided link. I have searched for a solution but have not found one that addresses my specific problem. Objective: Load HTML content to an element u ...

Encountered an unhandled rejection error: TypeError: Unable to destructure the property 'protocol' of 'window.location' because it is undefined

I integrated the react-tradingview-widget into my nextjs project and it was working perfectly on version 10.2.3. However, after upgrading to version 12.1.4, I encountered an error when trying to reload the tradingview component. The error message was: unh ...

Adjust size of element in relation to Background Cover using jQuery

Can you help me solve a challenge I'm facing? I have a website with a fullsize background image, and I need to attach a div to a specific position on the image while ensuring that it scales in the same way as the background image with the "background ...

Use percentages for the width in CSS and pixels for the height, ensuring that both dimensions are equal

I'm attempting to create a square that adjusts its size according to the screen size. Currently, I have the width set to 25%. Is there a way to ensure that the height remains the same length in pixels as the width? Appreciate any assistance on this ...

Utilizing Selenium and Python to extract data from JavaScript tables via web scraping

I've come across numerous articles about Web Scraping, but I'm still struggling to figure out how to locate specific elements on a website. The site where I want to scrape the tables can be found here: My target tables are: "TB01," "TB02," "TB0 ...

implement adding a division element to the DOM using the append

I am facing an issue with this particular code. The intended functionality is to create multiple divs in a loop, but it seems to be dysfunctional at the moment. Upon clicking, only one div appears and subsequent clicks don't trigger any response. < ...

"We encountered an error with the external script while trying to load file content using jQuery's ajax function. It

//C.php <script type="text/javascript"> $(document).ready(function(e) { $('div').load("D.php"); }); </script> <div></div> //D.php <script type="text/javascript" src="D.js"></script> //D.js console.log(45 ...

Here's how you can pull information from a MySQL database and showcase it on an HTML

I successfully created a database in MySQL where users can enter their details on /upload.html, which then get stored in the test2 database and person_list table. However, I am facing an issue with displaying this data on /download.html. My goal is to hav ...

I noticed that the node_modules folder has mysteriously vanished from my

When I tried running npm install in the terminal of VS Code. PS D:\work\backEnd> npm install npm WARN old lockfile npm WARN old lockfile The package-lock.json file was created with an older version of npm, npm WARN old lockfile so ...

Having trouble with JQuery's selectmenu() function?

I created a basic jsFiddle using jQuery UI .selectmenu() But for some reason, it's not functioning properly. Can anyone spot what I might have overlooked? <select name="filter" id="id_filter_log"> <option value="" selected>-- Select ...

Disable javascript when using an iPad

I'm working on a website with parallax scrolling that I don't want to function on iPads. Is there a way to prevent the parallax effect from happening when the site is accessed on an iPad? Any guidance on how to disable parallax scrolling based o ...

Transformation of CSS into SASS (or LESS)

Looking for recommendations on CSS to less or CSS to sass conversion tools. I've come across a couple like and , but unsure of their reliability. Any insights or recommendations on other tools would be highly appreciated. I have a sizable CSS code ba ...

Ways to activate jQuery validation when submitting a form instead of triggering it on blur

I am currently utilizing the Jquery Validation plugin to validate form fields upon submission. However, I have encountered an issue where incorrect email format triggers a validation message when moving to the next input field. This behavior is undesirable ...

Ensure that a function completes before moving on in JavaScript

I am attempting to modify the save method so that it waits for this.collection.create() to complete before running, in order to prevent a potential crash. class UserRepository extends BaseRepository<User> { constructor() { super(); ...

Challenges with synchronizing the scope of global arrays when reading JSON files into objects

I'm attempting to load a JSON file into a global array of objects. The code appears to be functioning correctly, however, I am encountering difficulty extracting the data from the Ajax $.getJSON call and storing it in the global variable. This issue c ...

The detailed record of this run can be accessed at:

npm ERR! code ENOTEMPTY npm ERR! syscall rename npm ERR! path /usr/local/lib/node_modules/expo-cli npm ERR! dest /usr/local/lib/node_modules/.expo-cli-dKBr48UN npm ERR! errno -39 npm ERR! ENOTEMPTY: The directory cannot be renamed because ...

Utilize the bootstrap grid to align content into 4 columns evenly

I have the following code snippet: <div class="panel-body"> <div class="col-sm-6"> <label class="head6">Business Name : </label><span class="head9">'.$name.'</span> </div> <div ...

The three.js pointLight has been updated from version 67 to version 68

There appears to be a change in the interaction between a pointlight and a plane from version r.67 to r.68. I am currently studying three.js by following along with a book that is a year old. I have simplified the tutorial example to include just a plane, ...