Reputation: 1281
I have this sample:
CODE HTML:
<div class="body">
<div class="test">TEST</div>
</div>
CODE JS:
switch (n) { //what I need to write instead of n
case body:
alert("my class is body");
break;
case another_class:
alert("my class is another");
break;
default:
//etc
}
I want simply to test have or not my class ... a class at a time, using a switch structure.
My pages on the site have different classes in body and want to apply a style only if there is a certain class ...
I hope you have understood what I meant if I did not try to explain otherwise
Thanks in advance!
Upvotes: 0
Views: 41
Reputation: 169
I'm not shure what you are trying to acchieve, but adding a certain style to a child element, when the parent element's class is different, can be done without any javascript, just declare your css like this:
.body .test{
styling of test inside .body in any case, overruled by anything that follows ;)
}
.body.classNameA .test{
styling of test in case of classNameA;
}
.body.classNameB .test{
styling of test in case of classNameB;
}
if the .body class is the thing that is changing, leave that out.
Upvotes: 0
Reputation: 3289
$('div').each(function(){
var Class=$(this).attr('class');
switch (Class) {
case "body":
alert("class is +"Class);
break;
case "test":
alert("class is +"Class);
break;
default:
//etc
}
});
This might help.
Upvotes: 0
Reputation: 74738
You can use this:
switch (document.querySelector('div').classList[0]) {
case "body":
alert("my class is body");
break;
case "another_class":
alert("my class is another");
break;
default:
//etc
}
Upvotes: 0
Reputation: 493
you could do like this in jquery:
if($('.body').length){
alert('this is body');
}
else if ($('.another_class').length)
{
alert('another');
}
Upvotes: 2