Spotting the top-tier class amidst the rest using jQuery - a guide

There is a single link containing various div classes:

<a class='div1 otherdiv alignleft'>Link</a>

I want to extract the first class of this link and store it in a variable. How can I achieve this without knowing the specific name of the first class?

Since the name of the first div class may vary, I cannot declare a fixed variable like var firstdiv = div1

Is there a way to retrieve the name of the first class and save it in a variable for later use in a function? For example:

var firstdiv = ('a').first();

Then utilize this variable in a function:

$(firstdiv).click(function(){
    // my function
});

How can I accurately identify the name of the initial div within the a tag?

Answer №2

It's as easy as pie! Check out this helpful code snippet http://jsfiddle.net/hj8Vb/

HTML Code Sample

<div id="bla" class="div1 another align test">Content</div>

jQuery Script

$('#bla').click(function(){
    alert($(this).attr('class').split(" ")[0]);
});

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

checking the validity of serialized information in Ajax

I am facing a specific issue where I need to validate data before it is saved through an ajax call. When the user switches to a different URL, the save_ass_rub function is triggered. Within my application, there is a custom Window that allows users to inp ...

Ways to achieve text transparency with CSS without converting it to an image

Is there a way to have my navigation bar indicate the selected page by changing the text color and adding a black background, while making only the text inside the current page nav transparent? For example, you can see the effect here: sammarchant.co.uk/n ...

Limiting the width of text using CSS or PHP

When I generate text automatically using PHP from the database and put it into a div, the text ends up stretching beyond the boundaries of the div. Here's an example of what I'm trying to achieve: <div> <?php Generate_All_Text_Co ...

Do GPU-intensive animations get impacted by the CPU's load?

I have set up a special compositing layer for a div with the following unique styles: div { position: absolute; height: 50px; width: 50px; background: #900; top: 100px; left: 200px; will-change: transform; transform: translateZ(0); } Afte ...

Show individual JSON values from a Python response on a webpage using HTML

I have created a webapp using Flask for OCR on documents with layoutlm. The website features an upload system that accepts images and documents, which are then processed by a Python script for OCR. This script returns a JSON response containing field names ...

Avoiding the use of angle brackets in XSLT语

Looking to replace a sequence containing '.\s+\w+' with a line break This is what I currently have: <xsl:value-of select="replace($fdesc,'[.][ ]+\w+','<br/>')" /> However, I am encountering an er ...

A guide on updating the appearance of a list of comma-separated tags using HTML, CSS, and jQuery

Is there a way to replicate the functionality of the tag section on this website for creating question entries? How can this be achieved? ...

Discovering Xpath using the full path is still possible

Looking to extract the date from a Google search result. I used the Xpath generated by Chrome-Xpath-Finder, but still unable to locate the element. How could this be? Using absolute path: from selenium import webdriver from selenium.webdriver.common.by imp ...

After pressing submit, any associated links should automatically open in a separate tab

I have a homework assignment that requires creating a form with multiple elements, including a checkbox. Once this checkbox is checked, the links displayed on the resulting page should open in a new tab. The catch? I can only use HTML and CSS - no JavaScri ...

What is the reason the .clearfix class fails to properly clear a floated element?

Check out this post: Understanding clearfix The most helpful response indicates that the clearfix should be applied directly to the last floating element: To incorporate a clearfix, you simply need to include HTML code: <div class="clearfix"> ...

What is causing .attr('title') to retrieve the title of the element before?

UPDATE Flexslider now includes several callback spaces for use, making this functionality possible. after: function(){}, //Callback: function(slider) - Fires after each slider animation completes Thank you to everyone who contributed! LIVE CO ...

What causes the browser to freeze while loading HTML content from an iframe into its parent element?

After successfully resolving the initial issue, I have now rephrased the question to inquire about the root cause of the problem. The following code snippet is what I am currently working with: <!DOCTYPE html> <html> <head> <meta h ...

Activate Input by Clicking on Label in Internet Explorer version 8

I am trying to implement a widget in JQuery colorbox that allows users to load files. My approach involves using an input tag with type='file' and styling it with visibility:hidden. Additionally, I have created two labels that are styled like but ...

Issue with Angular's ng-show failing to display an image post-processing

$scope.showProcessedGraph = function(hash) { if ($scope.selectedFile.hash == hash) return; $scope.graph = ""; $scope.isImageLoaded = false; $scope.isLoading = true; if (hash) { $http({ m ...

A common occurrence is for jQuery/Javascript Ajax POST to generate 6 identical posts when used within a .each loop, happening approximately 20

Context: Building a jQuery mobile phonegap application with multipage-ajaxload and sisyphus enabled form that utilizes an AJAX POST loop to interact with a GUI database. The process involves posting 171 section entries, along with one summary entry to a se ...

Bootstrap 4's Grid System: Embracing the Square Grid

Is there a way to create a dynamic grid of squares in Bootstrap 4? I understand the concept of creating a grid using <div class="row"> <div class="col-md-3" style="width: 100px; height: 100px; color: red"></div> <div class="col-m ...

`Unable to process JSON information from Google Places`

I am currently working on retrieving a rating for a specific place from Google using a JSON request by placeID. The fields I am requesting are the name and rating of the place. Upon viewing the JSON URL in my browser, I can see the data displayed as follow ...

What is preventing me from accessing the hidden article using the URL with the hashname?

I've been searching for a solution, but so far nothing seems to work for me. I have a page with header and content sections. The content section is currently not visible. When I click on an item in the navigation menu, I want to hide the header and di ...

issue with accessing global variable within class block

While the Python code within the 'outer' function runs smoothly on its own, placing it inside a class seems to cause issues that are difficult for me to comprehend. (I am aware that I could simply pass data as a variable inside the function or d ...

"The Ajax function for replacing a div upon change event is not functioning properly

I am having an issue with my select box using the onchange event, <select class="form-control" onchange="getval(this.value,'<?php echo $prd->pr_id;?>','ajax<?php echo $key?>','<?php echo $key ?>')"&g ...