0

html....

first case

<ul>
<li class="one two"></li>
<li class="nav three active"></li>
<li class="four nav4"></li>
</ul>

second case

<ul>
    <li class="one two"></li>
    <li class="nav three"></li>
    <li class="four nav4 active"></li>
    </ul>

Trying to select class starting with nav like this but seems wrong statement:

var $myclass = $('ul .active');
var $thisclass = $myclass.attr('[class^="nav"]'); // this line 
Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231

7 Answers7

0

You can use .hasClass()

var $myclass = $('ul .active');
$myclass.hasClass('nav')
Praveen
  • 55,303
  • 33
  • 133
  • 164
0

Use .has()

$myclass.has('li[class^="nav"]')
Dipesh Parmar
  • 27,090
  • 8
  • 61
  • 90
0
$('li').filter(function(){ 
    $(this).attr('class').split(' ')[0] == 'nav'
})
Tomzan
  • 2,808
  • 14
  • 25
0

Using the ^= operator will only select the nav if it's the first class in the class list:

<li class="nav4 .."></li>

You can include the *= operator to search for it in all the list:

var myClass = $('[class^="nav"], [class*=" nav"]');

Heres an example.

Side note: This is how most icon fonts use a single class icon-

hitautodestruct
  • 20,081
  • 13
  • 69
  • 93
0

You can use below single line of code to achieve your required result:

$('li.active*[class*="nav"]')

This will get you all those li elements, which:
1. have active as one of its class, AND
2. have any class in them which starts with nav (i.e. nav, nav4, nav<whatever>)

for demo please visit http://jsfiddle.net/purnil/dHRkS/2/

Purnil Soni
  • 831
  • 4
  • 18
0

You can use below jquery expression

$('ul').find('li[class*="nav"]').html('abc');

Please manipulate expression according to your requirement.

Also check out fiddle

Chirag Vidani
  • 2,519
  • 18
  • 26
-1

you can directly select it using

$(".nav")
Ganesh Pandhere
  • 1,612
  • 8
  • 11
  • This wont work as there is no element with class="nav". What @C-Link is trying to achieve is to get all elements with class name starting with "nav". – Purnil Soni Sep 12 '13 at 06:38