CSS Apprentice CSS Apprentice - 7 months ago 20
Javascript Question

How To Convert A className Into An ID in jQuery

In my example I have a basic tabbed content setup. The buttons have classes that match the divs IDs. onClick, I'd like to grab the className, and find an ID with the same name

enter image description here

jQuery:

$("li").click(function() {
var liClass = this.className;
$("li").removeClass("active");
$("#"+liClass).addClass("active");
});


jsFiddle: https://jsfiddle.net/CSS_Apprentice/kpo7t6xh/

Answer

Here's what's wrong:

 $("li").removeClass("active");

Should be:

 $("div").removeClass("active");

The `.active class was on the divs not the lis.

FIDDLE