Can you extract a portion of a CSS class and convert it into a string

Let's imagine a scenario where we have 4 divs:

<div class="mine-banana"></div>
<div class="mine-grape"></div>
<div class="mine-apple"></div>
<div class="orange"></div>

After some experimenting, I've managed to identify a JQuery selector that targets the 3 "mine" divs specifically. However, I'm now faced with the challenge of extracting all the fruits related to "mine" :)

$('[class*=" mine-"]').each(function() {
    var fruit = $(this).????
});

Answer №1

Check out the DEMO here

To achieve this, it's recommended to reorganize your html structure, as mentioned by @Ian. Here's an example:

<div class="mine" data-mine-fruit="banana"></div>
<div class="mine" data-mine-fruit="grape"></div>
<div class="mine" data-mine-fruit="apple"></div>
<div class="orange"></div>

With the updated HTML, the JS code becomes much simpler:

var fruits = $('.mine').map(function () {
    return $(this).attr('data-mine-fruit')
});

If you prefer to stick with your current code, you can use a regex-based approach like this:

var fruits = []
$('[class*=" mine-"], [class^="mine-"]').each(function() {
    fruits.push( this.className.match(/[^a-z0-9_-]?mine-([0-9a-z_-]+)/i)[1] );

})

This alternative method indeed gets the job done effectively.

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

Removing a row from a table in a React component

I have incorporated a Table component from Material UI into my project to display data fetched from an external API. The table dynamically updates its rows based on events received through a web socket connection. One specific requirement I have is that wh ...

The function addEventListener is not found

When a button is pressed, I want to add a value into a container by adding an event listener. <div className="grid-container"> <div> <button id="redBet" className="redButton" onclick={thi ...

What is the best way to trigger ajax when the user clicks the back button to return to the previous webpage?

Check out my code snippet below: HTML Code <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="body"> <div class="dropdown_div"> <select id="q_type" class="dropdown" ...

Update a variable globally within setInterval function

Seeking assistance on showcasing the number of seconds that have elapsed using a setInterval function in JavaScript integrated within HTML. The code snippet below has been attempted, but it is only displaying 1 second and the test function appears to not ...

What is the best way to create a gradual color change in each individual cell instead of changing all at once?

Looking for some help with a grid I'm working on. I've got the cells changing colors like I want them to, but they're all changing at once. What I really need is for them to light up in a specific order - Yellow, Green, Blue, White, Orange. ...

Steps for accessing documents stored in Sharepoint Document library folders

I have a unique setup in my document library: two folders are automatically created based on the year when files are uploaded. Now, I need to retrieve files from multiple folders using JavaScript... Here is my function for uploading a file and creating a ...

Attempting to grasp the intricacies of HTML5/JS video playback quality

I've been diving deep into research on this topic, but I can't seem to find a straightforward answer to my specific query. My main focus is understanding the inner workings of how video players transition between different quality settings (480p, ...

What seems to be the issue with the useState hook in my React application - is it not functioning as

Currently, I am engrossed in a project where I am crafting a Select component using a newfound design pattern. The execution looks flawless, but there seems to be an issue as the useState function doesn't seem to be functioning properly. As a newcomer ...

The object has been successfully loaded but is currently not visible

Currently, I am working with Nuxtjs and Threejs. The website's default script displays the geometry and animation correctly. However, I need to use a custom model, so I installed OBJLoader and MTLLoader using npm, which were successfully installed. & ...

Design a four-room apartment layout using three.js technology

My goal is to design a model of an apartment with four rooms. https://i.sstatic.net/3gPpG.jpg To achieve this, I have created a transparent cube to represent one room, but now I am facing challenges in adding the other three sections. https://i.sstatic. ...

Hide jQuery dropdown when mouse moves away

I am working on designing a navigation bar with dropdown functionality. Is there a way to hide the dropdown menu when the mouse pointer leaves both the navbar menu and the dropdown itself? I have tried using hover, mouseenter, and mouseleave but due to my ...

CSS: Overlapping pop-up obscuring another element

I am having an issue with my pop-up appearing behind another control, specifically an auto-complete text box. <div id="navTarget"> <ul class="menuTarget"> <li><a href="#"&g ...

JavaScript nested arrays not functioning with .map()

I've encountered a problem while using the .map function in JavaScript to extract a nested array from an API response. Here is the JSON: [ { "id": 3787, "title": "Dummy title!", "start_time": "2020-04-25T16:54:00.000Z", ...

Issues with routing in Angular's ui-router when using the href attribute

I created a state and am attempting to navigate to another page. Despite no console errors, the navigation is not functioning as expected. index.html <body ng-app="app"> <a href='#/first-msg'>link</a> <div ui-view&g ...

Guide on developing a personalized validation system with Vuetify regulations for verifying the presence of an item

I'm currently working on my first CRUD web app using Vue 2 + Vuetify, but I've hit a roadblock while trying to add validation to a form. Specifically, I need to ensure that no item with the same title already exists in the database. You can view ...

What is the best way to create space between these form boxes for padding?

I am struggling to align two form boxes on the same line and another one below them. Despite my attempts with divs, I cannot seem to separate these two fields. I even tried inserting the   character between them, but to no avail. Below is the snippet ...

Updating the header title in React Navigation 5.x with dynamic changes

After upgrading my project to react navigation 5.x, I encountered an issue with setting the header title. In previous versions, we used to set it like this: static navigationOptions = ({ navigation }) => ({ title: 'find', }); However ...

Children of flex items sharing the same height

I'm struggling with creating a flexbox responsive grid and need some guidance. My goal is to make all the .block divs have equal height, with the .bottom div positioned at the bottom. The current solution achieves this, but when the h2 heading spans ...

Employing jQuery autocomplete for fetching values based on key (label) pairs

Can anyone assist me with the following request? I am working with a list of names (places) and their respective coordinates: let data = [ {"location":"Zurich", "coordinates":"25.674847,85.025781"}, {"location":"Antwerp", "coordinates":"23.765237, ...

symfony submit form without sending request

I'm trying to make a request without using a submit button, only by selecting an option. So far, I've attempted to achieve this using JavaScript but haven't had any success. Here's my form code: $form = $this->createFormBuilder() ...