/    Sign up×
Community /Pin to ProfileBookmark

Drop Down Menu Problem

Hi,
‘m working on the dropdown menu. However, there is a problem. Search Box only works for the first ul element. Other data is not filtered. For example, D2 cannot be found while searching. No matter how much I researched, I could not find a solution. Waiting for your help. The code I wrote:

“`
<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”UTF-8″>
<meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
<title>Document</title>
</head>
<body>
<input type=”text” id=”inputText” onkeyup=”inputFunction()” placeholder=”Search… ” title=”Type in a name”>
<nav class=”menueidown vertical”>
<ul class=”categorydownulortak” id=”inputUl”>
<li data-dropdown>
<a href=”#”>
A
</a>
<div class=”dropdown”>
<ul>
<li data-dropdown>
<a href=”#”>
B
</a>

<div class=”dropdown”>
<ul>
<li>
<a href=”#”>C</a>
</li>
<li data-dropdown>
<a href=”#”>C2</a>
<div class=”dropdown”>
<ul>
<li>
<a href=”#”>D</a>
</li>
<li>
<a href=”#”>D2</a>
</li>

</ul>
</div>
</li>

</ul>
</div>
</li>
</ul></div>
</nav>
<script>
function inputFunction() {
var input, filter, ul, li, a, i, txtValue;
input = document.getElementById(“inputText”);
filter = input.value.toUpperCase();
ul = document.getElementById(“inputUl”);
li = ul.getElementsByTagName(“li”);
for (i = 0; i < li.length; i++) {
a = li[i].getElementsByTagName(“a”)[0];
txtValue = a.textContent || a.innerText;
if (txtValue.toUpperCase().indexOf(filter) > -1) {
li[i].style.display = “”;
} else {
li[i].style.display = “none”;
}
}
}
</script>

</body>
</html>
“`

to post a comment
HTML

3 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumDec 29.2020 — Your code makes all li's invisible that do not match, including the parent li's of the matching li. You need to make all li's that are parents of the matching one visible:
function inputFunction() {
var input, filter, ul, li, a, i, txtValue;
input = document.getElementById("inputText");
filter = input.value.toUpperCase();
ul = document.getElementById("inputUl");
li = ul.getElementsByTagName("li");
for (i = 0; i &lt; li.length; i++) {
a = li[i].getElementsByTagName("a")[0];
txtValue = a.textContent || a.innerText;
if (txtValue.toUpperCase().indexOf(filter) &gt; -1) {
li[i].style.display = "";
// make all parent li's visible:
// start with current li
let prnt = li[i];
// while parent li is available
while (prnt = prnt.closest('ul').closest('li')) {
// make it visible
prnt.style.display = "";
}
} else {
li[i].style.display = "none";
}
}
}
Maybe there is a more sophisticated way by using a recursive function but I didn't figure out that.
Copy linkTweet thisAlerts:
@SempervivumDec 29.2020 — PS: Coded the alternative way using a recursive function:
function inputFunction() {
function findIt(startEle) {
// get children of start element
const children = startEle.children;
let matched = false;
// for each child:
children.array.forEach(item =&gt; {
// check if there are matching successors
let matched = findIt(item);
// if current element matches: set matched to true
txtValue = item.textContent || item.innerText;
if (txtValue.toUpperCase().indexOf(filter) &gt; -1) {
matched = true;
}
// is any of the successors or the current element matching?
if (matched) {
// make it visible
li[i].style.display = "";
} else {
// hide it
li[i].style.display = "none";
}
});
// return matched for use in calling function
return matched;
}
}
// start with toplevel ul
findIt(document.querySelector('#inputUl'));
Seems to be more compact doesnt't it?
Copy linkTweet thisAlerts:
@wpdevelislauthorDec 29.2020 — @Sempervivum#1626277

Thank you very much.
×

Success!

Help @wpdevelisl spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...