Bé Bụng Bự
Bé Bụng Bự

Reputation: 3

How to change class name tag a with specific href?

Here my problem.

<a class="selected" href="#tab-general">General Setting</a>
<a href="#tab-setting1">Method 1</a>

This time, it's auto selected #tab-general. Now after click some button I want it auto select the #tab-setting1. I already try this thing but it didn't work:

var text='#tab-setting'+1;
$('.selected').attr('class','');
$('a[href$=text]').attr('class', 'selected');

Thanks for your help.

Upvotes: 0

Views: 2206

Answers (1)

hemanth
hemanth

Reputation: 1043

Text is a variable. So change this

$('a[href$=text]').attr('class', 'selected'); //this selector does not read the value of variable `text`

to

$('a[href$='+text+']').attr('class', 'selected');

I would suggest you to do something similar to below.

Html:

<a class="link selected" href="#tab-general">General Setting</a>
<a class="link" href="#tab-setting1">Method 1</a>

Jquery:

$('.link').on("click",function(){
    $('.link').removeClass("selected");
    $(this).addClass("selected");
})

Fiddle

Upvotes: 3

Related Questions