matching an element's class to another element's ID, or *part* of another element's ID
Posted
by shecky
on Stack Overflow
See other posts from Stack Overflow
or by shecky
Published on 2010-04-21T16:39:41Z
Indexed on
2010/04/21
16:43 UTC
Read the original article
Hit count: 357
hello again
Such a simple concept but I'm struggling to express it ... apologies in advance for my verbosity.
I have a container div with a class, e.g., ; I want to use that class to do two things:
- add a class (e.g., 'active') to the nav element whose ID matches the class of div#container (e.g., #nav-primary li# apples)
- add the same class to another element if part of this element's ID matches the class of #container (e.g., div#secondary-apples)
I assume there's an .each() loop to check the primary nav's list items' IDs, and to check the div IDs of the secondary nav ... though the latter needs to have its prefix trimmed ... or should I say more simply if the secondary nav div IDs contain the class of div#container?
I've tried a few variations of something like this:
<script type="text/javascript">
$(document).ready(function() {
$('#nav-primary li').each(function(){
var containerClass = $('#container').attr('class');
var secondaryID = $('#nav-primary li').attr('id');
// something like
if ('#nav-primary li id' == (containerClass)
{
}
// or should I first store a variable of the LI's ID and do something like this:
if ( secondaryID == containerClass )
{
}
// and for the trickier part, how do I filter/trim the secondary nav div IDs, something like this:
var secondaryNavID = $('#aux-left div[id ... something here to strip the 'secondary-' bit ... ]');
}); // end each
}); // end doc.ready.func
</script>
The markup is, e.g.:
...
... ... ...... ... ...
Many thanks in advance for any suggestions, svs
© Stack Overflow or respective owner