I want to be able to click on the <i>, the <a>, or the <li> and get all the <li>s.
The point is to detect the closest repetitive element no matter how deep in it's children you click.
This is what I have so far: It shows the count of the siblings but what I'd like to isolate is same-ness.
For example: <body> shows 2 siblings but they are not both <body>. <header> shows 4 but they not all <header>. How do I only count same siblings. In this case <li> but can be anything.
//Run in full-page↗️
$('body').on('click', (e) => {
$el = $(e.target);
$parentsAndSelf = $el.parents().addBack();
$parentsAndSelf.each( (i,el) => {
if( $(el).siblings().addBack().length > 1){
console.log( el, $(el).siblings().addBack().length );
//return false;
}
});
});
* {
padding: 5px;
margin:5px;
outline: 1px solid pink;
list-style-position: inside;
}
a {
display: block
}
.highlight{
background:yellow;
}
*:hover {
outline: 1px solid red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<header>
HEADER:
<nav>
NAV:
<ul>
UL:
<li>li <a href="#">item <i>1</i></a> </li>
<li>li <a href="#">item <i>2</i></a> </li>
<li>li <a href="#">item <i>3</i></a> </li>
<li>li <a href="#">item <i>4</i></a> </li>
</ul>
</nav>
</header>