/    Sign up×
Community /Pin to ProfileBookmark

JS function to display child elments of the class onclick doesn’t work

__(Added `[code]…[/code]` tags ~ MOD)__

I have mobile menu in which categories and subcategories are displayed one after another in the sequences. The categories are <li>s of one <ul>, and subcategories are <ul>s of each category <li>, and they have own <li>s of subcategory list like below:
Categories

[code]
<ul>
<li class=”cats” onclick = “Display()” >Car
<ul class=”subs”>
<li>New car<li>
<li>Rent car<li>
<li>Car parts<li>
</ul>
</li>
<li class=”cats” onclick = “Display()” >Digital
<ul class=”subs”>
<li>Notebooks<li>
<li>Tablets<li>
<li>Phones<li>
</ul>
</li>
</ul>
[/code]

By default, I do not display subcategories in menu

[code]
.subs{
display: none;
}
[/code]

And when clicking on any of the categories, that category’s subcategories must be seen.
I tired this function in JS which didn’t work.

[code]
function Display(){
var catitems = document.getElementsbyClassName(‘cats’);
var i;
for(i =0; i <catitems.length; i++){
catitems[i].children.style.display = “block”; (also tried this: catitems[i].firstChild.style.display = “block”;
}
}
[/code]

Please, ignore letter and similar minor mistakes here as I hadn’t problem with them in Visual Studio.
Why doesn’t it work? What would be JS solution for this?

to post a comment
CSSHTMLJavaScript

13 Comments(s)

Copy linkTweet thisAlerts:
@daveyerwinJun 14.2019 — <ul>

<li class="cats" onclick = "Display()" >Car

<ul class="subs">

<li>New car</li>

<li>Rent car</li>

<li>Car parts</li>

</ul>

</li>

<li class="cats" onclick = "Display()" >Digital

<ul class="subs">

<li>Notebooks</li>

<li>Tablets</li>

<li>Phones</li>

</ul>

</li>

</ul>

<script>

function Display(){

var catitems = document.getElementsByClassName('cats');

var i;

var collection1=catitems[0].children[0].children;

var collection2=catitems[1].children[0].children;

for(i =0; i <collection1.length; i++){
collection1[i].style.color = "red";
}
for(i =0; i <collection2.length; i++){
collection2[i].style.color = "green";
}

}

</script>
Copy linkTweet thisAlerts:
@daveyerwinJun 14.2019 — using children collection is not a good answer

use getElementsByTagName instead
Copy linkTweet thisAlerts:
@Farid999authorJun 14.2019 — Thanks Davey, but note that in the sample I showed 2 categories, but actually I have many categories like these. So, using first [0], second [2], etc. elements of collection is not the way I want. My website is written dynamically, so we need to find some generic function to make it work for each category separately.

Using getElementsByTagName also seems not a good way as I'll need to open subcategory <ul> of particular category <li> when clicking on that <li>. If written by tag name, it will again open all <ul>s of <li>s when clicking on any <li> like it was with class name.

If you know the way with getElementsByTagName that can make it work like I want, please, let me know.
Copy linkTweet thisAlerts:
@daveyerwinJun 14.2019 — <style>

.subs{

display: none;

}

</style>

<ul>

<li class="cats">Car

<ul class="subs">

<li>New car</li>

<li>Rent car</li>

<li>Car parts</li>

</ul>

</li>

<li class="cats">Digital

<ul class="subs">

<li>Notebooks</li>

<li>Tablets</li>

<li>Phones</li>

</ul>

</li>

</ul>

<script>

(function(){

var active;

var catsCollection = document.querySelectorAll('.cats');

for(var i=0;i<catsCollection.length;i++){

catsCollection[i].onclick=function(){

if(typeof active != 'undefined') active.style.display = '';

active=this.getElementsByTagName('UL')[0];

active.style.display = 'block';

}

}

}())

</script>
Copy linkTweet thisAlerts:
@JMRKERJun 15.2019 — Minor typo in last post. To make it work, change to:
<i>
</i> catsCollection[i].onclick=function(){
Copy linkTweet thisAlerts:
@daveyerwinJun 15.2019 — @JMRKER#1604797

Thank You !
Copy linkTweet thisAlerts:
@Farid999authorJun 15.2019 — Guys, this function didn't work. Also, I couldn't get the parenthesis () before the last one, it doesn't belong to any anonymous function or something else.

Any other idea?
Copy linkTweet thisAlerts:
@daveyerwinJun 15.2019 — this works as expected

sorry about the typo

may this will be more obvious
``<i>
</i>
&lt;style&gt;
.subs{
display: none;
}
&lt;/style&gt;
&lt;ul&gt;
&lt;li class="cats"&gt;Car
&lt;ul class="subs"&gt;
&lt;li&gt;New car&lt;/li&gt;
&lt;li&gt;Rent car&lt;/li&gt;
&lt;li&gt;Car parts&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li class="cats"&gt;Digital
&lt;ul class="subs"&gt;
&lt;li&gt;Notebooks&lt;/li&gt;
&lt;li&gt;Tablets&lt;/li&gt;
&lt;li&gt;Phones&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;script&gt;
(function(){
var active;
var catsCollection = document.querySelectorAll('.cats');
for(var i=0;i&lt;catsCollection.length;i++){
catsCollection[i].onclick=function(){
if(typeof active != 'undefined') active.style.display = '';
active=this.getElementsByTagName('UL')[0];
active.style.display = 'block';
}
}
}())
&lt;/script&gt;<i>




</i>
``
Copy linkTweet thisAlerts:
@JMRKERJun 15.2019 — I modified the catsCollection selection a bit with a for...of command and thus eliminating the extra variable 'i'.

I also added a close button to reset the displays to their original condition.

Why? Just for the fun of playing around with the code ... :)
<i>
</i>&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;meta charset="UTF-8"&gt;
&lt;meta name="viewport" content="width-device-width,initial-scale=1.0, user-scalable=yes"/&gt;
&lt;title&gt; active assignments &lt;/title&gt;
&lt;!-- From: https://www.webdeveloper.com/d/384882-js-function-to-display-child-elments-of-the-class-onclick-doesn-t-work/5 --&gt;
&lt;style&gt;
.subs { display: none; background-color: white; }
#Xclose { float: right; background-color: red; }

#list { width: 10em; border: 1px solid blue; cursor: pointer; }
#list li:hover { background-color: orange; }
#list ul li:hover { background-color: cyan; }

ul { list-style-type: none; padding: 0 0 0 0.1em; }
ul li ul { padding: 0 0 0 1em; }
&lt;/style&gt;
&lt;body&gt;
&lt;ul id="list"&gt;
&lt;button id="Xclose"&gt;X&lt;/button&gt;

&lt;li class="cats"&gt;Car
&lt;ul class="subs"&gt;
&lt;li&gt;New car&lt;/li&gt;
&lt;li&gt;Rent car&lt;/li&gt;
&lt;li&gt;Car parts&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;

&lt;li class="cats"&gt;Digital
&lt;ul class="subs"&gt;
&lt;li&gt;Notebooks&lt;/li&gt;
&lt;li&gt;Tablets&lt;/li&gt;
&lt;li&gt;Phones&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;

&lt;/ul&gt;
&lt;script&gt;
( function() {
var active;
var catsCollection = document.querySelectorAll('.cats');
for (let cat of catsCollection) {
cat.onclick=function(){
if(typeof active != 'undefined') active.style.display = '';
active=this.getElementsByTagName('UL')[0];
active.style.display = 'block';
}
}
document.getElementById('Xclose').addEventListener('click', () =&gt; active.style.display = '');
}())
&lt;/script&gt;

&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@Farid999authorJun 17.2019 — @JMRKER#1604805

Thanks for your code, JMRKER.

When it comes to close button, how can we do it so that after re-clicking the relevant category (<li>), it would close its subcategories (<ul>).

I tired this, but didn't work.

(function(){

var active;

var catsCollection = document.querySelectorAll('.catlist');

for(var i=0;i<catsCollection.length;i++){

catsCollection[i].onclick=function(){

if(typeof active != 'undefined') active.style.display = '';

active = this.getElementsByTagName('UL')[0];

if(active.style.display == ' ' )

active.style.display = 'block';

else

active.style.display = ' ';

}

}

}())

Any idea?



Copy linkTweet thisAlerts:
@daveyerwinJun 17.2019 — more playing around with code ...

``<i>
</i>&lt;style&gt;
.subs{
display: none;
}
&lt;/style&gt;
&lt;ul&gt;
&lt;li class="cats"&gt;Car
&lt;ul class="subs"&gt;
&lt;li&gt;New car&lt;/li&gt;
&lt;li&gt;Rent car&lt;/li&gt;
&lt;li&gt;Car parts&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li class="cats"&gt;Digital
&lt;ul class="subs"&gt;
&lt;li&gt;Notebooks&lt;/li&gt;
&lt;li&gt;Tablets&lt;/li&gt;
&lt;li&gt;Phones&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;script&gt;
(function(){
var active;
var catsNodeList = document.querySelectorAll('.cats');
[].forEach.call(catsNodeList, function(item, index, array) {
item.onclick=function(){
if(typeof active != 'undefined') active.style.display = '';
if(this.getElementsByTagName('UL')[0] == active){
active.style.display = '';
active = undefined;
}else{
active=this.getElementsByTagName('UL')[0];
active.style.display = 'block';
}
}
})
}())<i>
</i>
``
Copy linkTweet thisAlerts:
@daveyerwinJun 17.2019 — the above closes the ul when it is clicked while open

I left off the closing </script>

sorry about that::
Copy linkTweet thisAlerts:
@Farid999authorJun 17.2019 — @DaveyErwin#1604878 ,

Thank you very much, it has worked.
×

Success!

Help @Farid999 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,
)...