Barış Velioğlu
Barış Velioğlu

Reputation: 5817

Show the element if it has a class whose name is tested

I got a class name after I clicked a element in the document. And then I want to select all elements that has this selected class. For example;

$('.tabs1 li a').hover(function(){

var clicked = $(this).attr("class");

// this doesnt work
$('a[class="clicked"]').css("display","block");

});

Upvotes: 0

Views: 94

Answers (2)

g_thom
g_thom

Reputation: 2810

It's true that the class attribute can have multiple values. Like others, I'm guessing to an extent what the poster wants, but this at least allows for multiple classes (and accounts for spaces). If the poster's wanting to isolate just one of the classes, maybe that's another question or subquestion:

$('.tabs1 li a').hover(function(){
    var clicked = $(this).attr("class");
    clicked = clicked.replace(/(\s)+/g, '.');
    // this should work
    $('a.' + clicked).css({"display":"block", "background": "green"});
});

http://jsfiddle.net/szpJb/2/

Upvotes: 3

Frédéric Hamidi
Frédéric Hamidi

Reputation: 262939

You can build a class selector from the class name:

var clicked = $(this).attr("class");
$("a." + clicked).css("display", "block");

Upvotes: 3

Related Questions