Bhupendra Pandey
Bhupendra Pandey

Reputation: 348

jquery adding class dynamically

i have a ul

<ul id="main-menu">
<li class=" mySelectedMenu"><a title="Dashboard" href="/">Dashboard</a> </li>
<li class=""><a title="Inventory" href="/inventories">Inventory</a></li>
<li class=""><a title="PJPS" href="/pjps/oldindex">PJPS</a></li>
<li class=""><a title="Reports" href="#">Reports</a></li>
<li class=""><a title="Today" href="#">Today</a></li>
<li class=""><a title="Discounts" href="/discounts">Discounts</a></li>
</ul>

dynamically i got title den i have to add class active in that <li> which title is same as that which i received. so how can i do??

Upvotes: 2

Views: 1564

Answers (3)

$('ul li a').click(function() {
var classname = $(this).attr('title');
$(this).closest('li').addClass(classname);
});

try this. Hope it will help u

Upvotes: 0

bipen
bipen

Reputation: 36531

use addClass() to add a class... and attribute selector [] to select the element

$("a[title="+yourTitle+"]").parent().addClass('active');

try it in fiddle

Upvotes: 3

Arun P Johny
Arun P Johny

Reputation: 388316

Try

$('#main-menu li a[title=' + title + ']').parent().addClass('active')

Demo: Fiddle

Upvotes: 2

Related Questions