Ways to distinguish between two labels having the same "for" attribute?

My HTML code is as follows:

<label class="my-class" for="_something">Text Number 1</label>
<label class="my-class" for="_something">Number 2 Text</label>

I have two labels with the same class and identical "for" attributes.

I am aware that I can target the attribute using this CSS rule:

label[for="_something"] {
   /* input */
}

But how can I distinguish between the two?

I specifically need to hide the second label using CSS.

Is it possible to target the attribute based on the text content like "Number 2 Text" in this case?

Answer №1

Utilizing CSS, it is definitely achievable. Simply utilize the nth-of-type() selector.

Here's an example of the HTML structure:

<label class="my-class" for="_something">Text Number 1</label>
<label class="my-class" for="_something">Number 2 Text</label>
<label class="my-class" for="_something">Number 3 Text</label>
<label class="my-class" for="_something">Number 4 Text</label>

The corresponding CSS code would be:

label[for="_something"]:nth-of-type(2) {
  display: none;
}

Explore the solution further on PHPFiddle: http://jsfiddle.net/ua91bf0b/

Note that this technique relies on a CSS3 feature and may not be supported by outdated browsers.

Answer №2

If you wish to conceal the label depending on its content (Text with Number 2), jQuery is your best bet. It operates similarly to using CSS selectors.

$('label[for$="_something"]:contains("2")')

For instance, this snippet targets labels where the 'for' attribute's value ends with "_something" and includes the text "2".

To simplify usage, you can create a function like this:

function HideLabel(forAttribute, searchText) {
  $('label[for$="'+forAttribute+'"]:contains("'+searchText+'")').css('display','none');
}

You can then call this function as follows:

HideLabel('_something', '2');

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

Divide an image into several sections without the need to reload the image multiple times

I am looking for a way to divide an image into separate "portions" without having to load the image multiple times to save bandwidth. In this case, I want to split the image into 4 quadrants, but ideally, I would like a scalable solution. https://i.sstat ...

Position the Bootstrap button at the bottom left with absolute placement

Currently, I am utilizing Django as my web framework and Bootstrap to enhance the rendering of the frontend. My goal is to implement an "add" button on my page that will always be in the bottom left corner regardless of scrolling, overlaying any other cont ...

How come the changes in my react component's CSS are not updating in real-time?

After integrating redux into my create-react-app project, I've been struggling to implement a navigator that highlights the active "page link." To achieve this functionality, I am using a combination of react hooks (to maintain the current page state) ...

The <main> element is not inheriting the height of its parent

https://i.sstatic.net/PchVf.png Using Chrome DevTools, I removed unnecessary elements from the DOM. The body is set to relative and takes up all available space in the document, which is exactly what I want. https://i.sstatic.net/AzELV.png My toolbar i ...

What could be causing my class to not be affecting a specific element in Sass?

Using React and sass, I attempted to utilize the activeClassName property on a NavLink component but found that my active class was not working as expected due to its order in relation to the default class. How can I resolve this issue? Here is the code: ...

Various designs for various arrangements in vuejs applications

In my project, I have two distinct layouts that function as separate parent routes with multiple nested components. I want to apply different global styles to each layout, but when I import styles for one layout, it affects the other as well: <style ty ...

Modify the parent's style in CSS based on the presence of certain specified children

I'm working with an HTML element that can contain a child with the ID 'slideshow' <div id='content'> <div id='slideshow'> </div> </div> Alternatively, it could have a different child like t ...

Issues arising from the combination of a grid system in Bootstrap and WordPress

There seems to be an alignment issue on my webpage where the div.col-4 inside div.col-9 is not aligning properly. To provide a better explanation, here is the code snippet and a screenshot of my webpage: <div class="col-12 col-lg-9"> < ...

Animate the menu as you scroll

I am attempting to create a jQuery top menu that adjusts its height based on the window scroll position. Using $(selector).css("height", "value") works fine, but when I try to animate it, it doesn't behave correctly. Here is my code. Thank you for you ...

Hover over elements to reveal D3.js tool tips

Currently, I am utilizing a tutorial for graph tooltips and finding it to be very effective: http://bl.ocks.org/d3noob/c37cb8e630aaef7df30d It is working seamlessly for me! However, I have encountered a slight problem... In the tutorial, the graph disp ...

Persist user input even after reloading the page

In order to enhance the user experience, I would like to implement a feature where the data entered by the user is preserved even if they refresh, reload, or close the page. This includes retaining the selections made in the select elements. Additionally, ...

Error Encountered: TemplateSyntaxError on the User Profile Page

I encountered an error: TemplateSyntaxError at /accounts/profile/ <ExtendsNode: extends "registration/accounts/base.html"> must be the first tag in the template The content of base.html is as follows: {% load staticfiles %} <htm ...

Achieving a seamless full-width background image without any distortion or cutting off

Is it possible to achieve a full width background without cropping, resizing, or repeating? When using background-size: cover, part of the image gets cut off, and with background-size: contain, it repeats on the right side. CSS: .home-3 { background ...

Creating nested tables by assigning unique Div IDs to each table element

Can we utilize a straightforward JavaScript/jQuery function to nest elements inside table elements? For instance, every square comes with its own unique ID (A1, B7, C5). How can I use this ID to insert the checker image in any selected tile upon clicking? ...

Numerous links were chosen and multiple div elements were displayed on the screen

Currently, I have a setup where selecting one div will show its content. However, I am looking to enhance this by allowing multiple divs to be displayed simultaneously. For example, if 'Div 1' is selected and shown, I want the content of 'Di ...

Organizing table data by columns in a continuous loop

I'm currently working on extracting data from a database, organizing it into tables (potentially multiple tables), and presenting them in columns of 4 across several pages. My main concern is figuring out how to display the tables horizontally like t ...

Disable list-group-item-action clicking

I'm looking to maintain the list-group-item-action hover effect that changes the background color while eliminating the click effect that activates it. Is there a way to achieve this? I've already removed the href="#" from the If I remove list-g ...

Troubleshooting Autocomplete User Interface Problems

I am currently working on a website user interface and I have encountered an issue specifically in IE7. When searching for a company, the display is not showing up correctly, as demonstrated in the image below. This problem only occurs in IE7, it works fi ...

Inequality in spacing between Bootstrap columns

I am currently working on a website where the home page has columns with icons and text. The issue arises on mobile devices as the columns stack unevenly due to varying text lengths, resulting in inconsistent vertical spacing. Is there a way to ensure cons ...

Function in jQuery to reference two different divs

I'm currently facing an issue with a code snippet that I have. The requirement is for the user to be able to hover over "Div 1" and/or "Div2" and trigger a red border around both elements simultaneously. Due to the complexity of my WordPress theme, th ...