Solved

Check class name of current CSS selector

Posted on 2014-11-11
5
173 Views
Last Modified: 2014-11-17
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">
	<ul>
		<li><a href="/dir1" id="dir1">Directory 1</a></li>
                <li><a href="/dir2" id="dir2">Directory 2</a></li>
	</ul>
</div>

Open in new window


I want to know that class was submenu and id was dir1 if first item clicked?
0
Comment
Question by:894359
  • 2
  • 2
5 Comments
 
LVL 8

Expert Comment

by:Ahmed Merghani
ID: 40436749
Hello 894359,

We can do it by using jQuery.
See the attached file.
test.html
0
 
LVL 82

Expert Comment

by:leakim971
ID: 40437213
test page : http://jsfiddle.net/8k7yo6u9/1/

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

Open in new window

0
 

Author Comment

by:894359
ID: 40438182
IS there a way without jQuery and only javascript?
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 40438294
here : http://jsfiddle.net/8k7yo6u9/2/

<div id="menu">
    <div class="submenu">
        <ul>
            <li><a href="/dir1" id="dir1">Directory 1</a></li>
            <li><a href="/dir2" id="dir2">Directory 2</a></li>
        </ul>
    </div>
    <div class="submenu2">
        <ul>
            <li><a href="/dir3" id="dir3">Directory 3</a></li>
            <li><a href="/dir4" id="dir4">Directory 4</a></li>
        </ul>
    </div>
</div>
<script>
    var links = document.getElementById("menu").getElementsByTagName("a");
    for(var i=0;i<links.length;i++) {
        links[i].onclick = function() {
            var className = this.parentNode.parentNode.parentNode.getAttribute("class");
            alert( className );
            return false;
        }
    }
</script>

Open in new window

0
 

Author Comment

by:894359
ID: 40439136
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?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now