0

I have some li's

 li class="views-row views-row-2 views-row-even"  
 li class="views-row views-row-3 views-row-odd"  
 li class="views-row views-row-4 views-row-even"  
 li class="views-row views-row-5 views-row-odd"  

Now on hover i am adding a class selected to corresponding li. Suppose i hover on first li element, selected classes get added to this li. So i will have
li class="views-row views-row-2 views-row-even selected". Now how do i get the views-row-2 class of this selected li.Similarly if second li is selected i need views-row-3 class and so on. Please help

JAB
  • 3,546
  • 9
  • 36
  • 39
  • 1
    Refer [this answer](http://stackoverflow.com/questions/1227286/get-class-list-for-element-with-jquery/1227309#1227309). Should answer your question. – dexter May 17 '11 at 04:48

3 Answers3

4

Several options, but this is a good one:

views_row = $("li.selected").attr("class").match(/(views\-row\-[0-9]+)/)[0];
dtbarne
  • 8,110
  • 5
  • 43
  • 49
  • Glad to finally contribute on SO a bit :) I should note that that will yield an error if there are no matches, so you should make sure to do a check that the array has a result before accessing it. – dtbarne May 17 '11 at 05:12
  • @dtbarne : hi dtbarne .. can you tell me why is [0] used in the end – JAB May 17 '11 at 05:57
  • String.match() returns an array, so that grabs the first match from the array. – dtbarne May 17 '11 at 06:18
1

You haven't mentioned what's special about the view-rows-2 class:

  • Do you already know which class you want?
  • Will it always be this class?

With what information you've given, here are my suggestions:

  1. You might want to check out .hasClass()

  2. If you want all classes for an element, see Get class list for element with jQuery

So if you want to get classes based on a prefix (which in your case seems to be views-row-, you could use what's suggested in the answers to these SO question: jQuery - Get a element class based on a prefix and jQuery selector regular expressions

Community
  • 1
  • 1
no.good.at.coding
  • 20,221
  • 2
  • 60
  • 51
  • in hasClass he has to pass whole class name i.e. views-row views-row-2 views-row-even selected not views-row-2. – Vivek May 17 '11 at 04:51
  • My objective is, suppose first li is selected i need views-row-2 class , similarly if 2nd li is selected i need views-row-3 class and so on – JAB May 17 '11 at 04:53
  • @JAB Ah, ok, so it seems like you want what I've suggested in #3 in my edited answer – no.good.at.coding May 17 '11 at 04:55
  • @Vivek, I don't understand. For `hasClass()`, he would use it something like `$('selector').hasClass('views-row-2')` which would return `true` or `false` based on whether the element had the class or not. – no.good.at.coding May 17 '11 at 04:56
0

you can use indeOf() something like this...suppose you have that selected class as you have written..

if(class.indexOf('views-row-2')!=-1){
    //do what ever you want to do, you can use sbstr() to get your class name too.
 }
Vivek
  • 10,978
  • 14
  • 48
  • 66