Ravichandran Jothi
Ravichandran Jothi

Reputation: 3066

How to check li classname is set using jquery?

<li><a class="" href="/apps/financials" title="Financials">Financials</a></li>
    <li><a class="" href="/apps/financials" title="Financials">Financials1</a></li>
    <li><a class="" href="/apps/financials" title="Financials">Financials2</a></li>
    <li><a class="" href="/apps/financials" title="Financials">Financials3</a></li>
    <li><a class="" href="/apps/financials" title="Financials">Financials4</a></li>
    <li><a class="" href="/apps/financials" title="Financials">Financials5</a></li>
    <li><a class="" href="/apps/financials" title="Financials">Financials6</a></li>

I have set of li's like above. I need to check for all the li's classname is set or not..if not set i need to set classname as "selected" for last li.

How can i do that?

thanks Ravi

Upvotes: 3

Views: 4392

Answers (8)

Treborbob
Treborbob

Reputation: 1221

Create a variable to store if you found a classname on an LI

Loop the LI's using jquery's Each, if the elements attribute class is blank, set your variable to true

After the loop, if your variable is true, set li:last to be 'selected'

Rough example

$(document).ready(function () {
  var liHasClass = false;
  $('Selector > li').each(function () {
    if($(this).attr('class') != '') {
      liHasClass = true;
    }
  });
  if(liHasClass == false) {
    $('selector li:last').addClass('selected');
  }
});

Upvotes: 0

jensgram
jensgram

Reputation: 31518

Not sure I understand, but I'll take a shot:

The following will add the class selected to the last <a> element if no <a> element already has the selected class.

var $anchors = $('li a');
if ($anchors.filter('.selected').length < 1) {
    $anchors.last().addClass('selected');
}

Alternative interpretation
If the class attribute is empty (or not set) for all <li> elements, add class selected to last <li> element:

if ($('li').filter(function() {
        return this.className && this.className.length > 0; // Include all elements with `.className` set and non-empty.
    }).length < 1) {
    $('li:last').addClass('selected');
}

Upvotes: 0

Johan Davidsson
Johan Davidsson

Reputation: 2972

if (!$("li").hasClass("selected")) {
    $("li:last").addClass("selected");
}

Upvotes: 1

Marc Uberstein
Marc Uberstein

Reputation: 12541

You can try the following:

$(function(){
 if (!$('li.selected').length >0)
    $('li:last').hasClass('selected');
});

Upvotes: 0

Semyazas
Semyazas

Reputation: 2101

if(!$('li').attr(class)){
    $('li:last').addClass("selected");}

Upvotes: 0

Eruant
Eruant

Reputation: 1846

If you are wanting to test a class you can use .hasClass() http://api.jquery.com/hasClass/

If you don't know what class you are testing for this wont work though.

Upvotes: 0

Talha Ahmed Khan
Talha Ahmed Khan

Reputation: 15483

Consider that we have all the LIs in a OL having class="myOL"

var lastLI = $('.myOL > li:last');
if( ! ( lastLI ).attr(class) ){
    lastLI.addClass("selected");
}

Upvotes: 0

genesis
genesis

Reputation: 50982

try .hasClass();

if ($("li").hasClass("someClass")){
    //yes it includes this class
}

Upvotes: 0

Related Questions