adam
adam

Reputation: 63

jQuery - How to print class name

This my example of my situation,I have a menu that using same class and want to print out name of class.

<a href="#" class="top" onClick="test()">Home</a> 
<a href="#" class="top" onClick="test()">Profile</a> 
<a href="#" class="top" onClick="test()">background</a>

Here I have tried to create a function but can't work.
The expected result will show "top selected"

function test(){
 var getClass = $(this).find('.top'); 
 if(getClass=='top'){
   alert(getClass +'selected');
  }
}

Upvotes: 0

Views: 5386

Answers (3)

Bhatiya J
Bhatiya J

Reputation: 1

<a href="#" class="top" onClick="test()">Home</a> 
<a href="#" class="top" onClick="test()">Profile</a> 
<a href="#" class="top" onClick="test()">background</a>

<p></p>

var className = $( 'a' ).attr( 'class' );
$( 'p' ).text(className);
// console.log(className);

Upvotes: 0

zgood
zgood

Reputation: 12571

Maybe something like this:

<a href="#" class="top" onClick="test(this)">Home</a>

and then...

    function test(el){
      var className = $(el).attr('class');
      console.log(className);
   }

Upvotes: 1

ZEE
ZEE

Reputation: 5849

var className = $('.top').attr('class');
console.log(className);

this should work

Upvotes: 2

Related Questions