Link to home
Start Free TrialLog in
Avatar of MJ
MJFlag for United States of America

asked on

Check class name of current CSS selector

If a user is clicking on a button or link, how do I capture the classname and ID of that link? What if the class name is in a parent element of that link? Example:
<div class="submenu">
		<li><a href="/dir1" id="dir1">Directory 1</a></li>
                <li><a href="/dir2" id="dir2">Directory 2</a></li>

Open in new window

I want to know that class was submenu and id was dir1 if first item clicked?
Avatar of Ahmed Merghani
Ahmed Merghani
Flag of Sudan image

Hello 894359,

We can do it by using jQuery.
See the attached file.
test page :

jQuery(function($) {
    $("a[id^=dir]").click(function(evt) {
        var className = $(this).closest("div").attr("class");
        // checking :
        alert( className );

Open in new window

Avatar of MJ


IS there a way without jQuery and only javascript?
Avatar of leakim971
Flag of Guadeloupe image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MJ


leakim, is there a way to bubble up to capture the first class name if you don't know the relationship of the class name?