One method you can use to test to see if the element exists in the document:
if($('.k-nav-current').length) {
$("#site-title").addClass("babycurrent");
}
of course if length is 0
, it is considered "falsey". any other positive number is "truthy".
However, for any further help, we will need to see the actual markup.
EDIT:
For your current setup, you are trying to use #main li:nth-child(2)
and checking its class. The class is actually applied to the anchor. You need to use #main li:nth-child(2) a
if ($("#main li:nth-child(2) a").hasClass('k-nav-current')) {
$("#site-title").addClass("babycurrent");
}
Though it might be better to just use something like this (considering ids are unique):
if ($("#baby").hasClass('k-nav-current')) {
$("#site-title").addClass("babycurrent");
}
Lastly, if you really want it dynamic, you can do something like this:
var current = $("a.k-nav-current")[0].id.replace('#','');
$("#site-title").addClass(current + "current");
EDIT:
$( document ).ready(function() {
if ($("#baby").hasClass('k-nav-current')) {
$("#site-title").addClass("babycurrent");
}
});
// or you can do this (commented out) /*
$( document ).ready(function() {
var current = $("a.k-nav-current")[0].id.replace('#','');
$("#site-title").addClass(current + "current");
});
*/