"Discovering the active span tag in a webpage: A step-by-step guide

I have a main anchor tag with two span tags inside, each with a different class name as shown below.

<a class="clickSlide" id="btnmain_login" href="/">
    <span class="Icohide"></span>
    <span class="Icoactive"></span>
</a>

When logged in, the span with the Icoactive class will be active. When not logged in, the span with the Icohide class will be active. How can I determine which span class is currently active using jQuery?

Any assistance would be greatly appreciated.

Answer №1

Simply target all span elements and utilize the hasClass method with the class parameter to determine which ones have the active class.

Check out this example

console.log($('span.Icoactive').hasClass('active')) // returns false
console.log($('span.Icohide').hasClass('active')) // returns true
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a class="clickSlide" id="btnmain_login" href="/">
  <span class="Icohide active"></span>
  <span class="Icoactive"></span>
</a>

Alternatively

$("span").each(function(index, element) {
  console.log(element.className);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a class="clickSlide" id="btnmain_login" href="/">
  <span class="Icohide active"></span>
  <span class="Icoactive"></span>
</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

What impact does the order of the element I'm specifying in my .css file have on its appearance after being rendered?

(An update has been added below) In my project, I am working with a .css file and an index.html document. The goal is to create a panel of buttons on the screen [to prototype the usability of a touchscreen interface], with each button assigned a specific ...

One of the quirks of Angularjs is that the ng-enter animation will only be

The initial animation only occurs the first time. I am utilizing Angularjs version 1.2.22 Here is the CSS that I am using : .ng-enter { animation: bounceInUp 2s; } .ng-leave { animation: bounceOutUp 2s; } And this is the route configuration : ...

I am facing issues with my submit buttons as they are not functioning

Once I hit the submit buttons, there seems to be an issue with redirecting to another page. Could anyone assist in identifying the error within this code and why my buttons "typ1" and "cod" are not redirecting to the specified location? <?php inc ...

What is the best way to transform this unfamiliar CSS element into JavaScript code?

I'm facing an issue where I want to animate a CSS image based on time constraints, but since it's in CSS, I'm unable to achieve the desired effect. The animation in question is not the sun itself, but rather a yellowish half-circle animation ...

Make sure to validate a form when submitting from an external source rather than through an HTML

In my form, there are various input fields (some acting as inputs even if they're not). Each field is validated upon submission by angular validation and html attributes like required, ng-maxlength, minlength, etc. Now, we want to implement a keyboar ...

How to style an HTML table with just a bottom border

Is there a way to recreate the Staff Directory section of this page using only HTML, without having to rely on CSS and Javascript? Specifically, I want to create a table with only bottom borders/dividers for each line. Any suggestions on how to achieve thi ...

Issue with dynamic elements created through Ajax and toggleClass() in .each() loop

After a successful call to .ajax(), I am generating a series of div's containing forecasted weather data. My goal is to initially display only the div with class="forecast-list", and then listen for the user's click on that element to reveal addi ...

The alignment of the Bootstrap grid is off-kilter

Utilizing the django framework, I incorporated a loop in my templates to showcase images from my database. Additionally, I integrated the Bootstrap grid system for a cleaner layout. However, there seems to be an issue with alignment, as some of the images ...

Stop the Text Table from being highlighted

My webpage includes a dynamic table where users can select multiple rows. jQuery events and CSS are utilized to provide visual feedback when a row is selected. However, pressing the shift key sometimes causes text to become highlighted, which is not idea ...

CSS selector for GTM trigger that is not specific to any particular element

I am attempting to develop a CSS selector that can target the specific entries within the page-top-menu: Header menu - Menu 1 DE #page-top-menu > div > div.menu-wrapper > ul > li.i39.dropdown.layer.item-type-1.catid-0eca5a6c6e53f281b9e0469ca3d ...

The change event for the select element is malfunctioning

Currently, I am deep diving into Nodejs with the second edition of the node cookbook. This book has caught my attention because it explains concepts using practical sample code, making it easier to grasp. The example code I am working on is related to Br ...

Exposing a Hidden Division with a Link via jQuery

I currently have a jQuery Panel set up to open when clicking a button. Now, I am looking to add a second link at the bottom of the page that will also open the same panel. Can anyone provide guidance on how to accomplish this? You can view my JSFiddle a ...

Looking for a way to customize your Datatables experience? How about moving

Currently, I am implementing DataTables from datatables.net and I have a specific request. I would like to place my search box outside of the table, possibly in my header div. Can this be achieved? ...

Is there a way for me to make the two form fields expand and fill the gap between them?

I've created a form with the first name and last name fields displayed horizontally next to each other, as shown in the image. When a user clicks on a field, it should change color (still pending coding). However, there seems to be an unwanted gap be ...

Guide to adding a Font to a server

Recently, I downloaded and installed a font on my computer and incorporated it into my project. While it appeared to work perfectly on my local system, I encountered issues when I uploaded the page to the server. How can I ensure that the font is properly ...

In a jQuery project, WebStorm marks all $-operators as "unrecognized."

Just a quick question from a beginner: I'm facing an issue where my WebStorm IDE doesn't recognize any jQuery code, even though the webpage functions correctly in the browser. Here's what I've done so far: I have installed WebStorm V ...

Lower the transparency of a container without affecting the transparency of its elements

Is there a way to decrease the transparency of the page contents container background without affecting the opacity of the actual content? <div id="container"> <div id="page contents"> Insert your amazing articles and more here. </ ...

Symfony2 requires clarification and direction when it comes to managing quantities in a shopping cart

My current challenge involves managing quantities in a shopping cart. I can easily add, delete, and view products in the cart, but am struggling with increasing or decreasing quantities if a user wants to purchase multiple units of the same product. BACKG ...

CSS - Desiring a border with three layers

Here is the CSS code I am using: border: 2px solid #00ff60; outline: 1px solid #000; outline-offset: 0px; This is how it looks: https://i.sstatic.net/SuvU1.png Is there a way to modify it so that it includes an inner black border, similar to the one at ...

IE throws an exception when attempting to use Canvas as it is not supported

One of my challenges involves working with a Canvas Element: <canvas id="canvas" width="300" height="300"> Sorry, your browser does not support the Canvas element </canvas> In my JavaScript file, I have the following code: var ct= docum ...