I am interested in displaying all the items on the list instead of just one

<html>
    <head>
        <link rel="stylesheet" type="text/css" href="mango.css">
        <script>
            function showItems(){
                var items = document.querySelectorAll("#main li");
                items.forEach(item => {
                    item.style.display = "block";
                });
            }
        </script>
    </head>
    <body>
    </body>
    <ul id="main" onclick="showItems()">main1
        <li>sub1</li>
         <li>sub2</li>
         <li>sub3</li>
    </ul>
     <ul id="main">main2
        <li>sub11</li>
         <li>sub2</li>
         <li>sub11</li>
    </ul>
</html>

Custom CSS:

#main li{
    list-style-type: none;
    display: none;
}

In the code provided, clicking on main1 only displays the first list item. I need a modification to show all list items under that section when clicked. Can you assist with this issue?

Answer №1

I have updated the code by replacing the id with class and adjusting the JavaScript accordingly. It is important to note that using multiple elements with the same id can lead to unexpected behavior across different browsers.

<html>
    <head>
        <link rel="stylesheet" type="text/css" href="mango.css">
        <script>
            function mango(){
                document.querySelectorAll(".apple")
                    .forEach(function(node){
                        node.style.display="block";
                    })                        
            }
        </script>
    </head>
    <body>
    </body>
    <ul id="main" onclick="mango()">main1
        <li class="apple">sub1</li>
         <li class="apple">sub2</li>
         <li class="apple">sub3</li>
    </ul>
     <ul id="main">main2
        <li>sub11</li>
         <li>sub2</li>
         <li>sub11</li>
    </ul>
</html>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Avoid working on the script for the element in the partial view during the event

In my index.cshtml view, I have a script that triggers an AJAX call when the SearchingManagerId2 element is changed. $("#SearchingManagerId2").on("change", function () { var valueForSearch = $(this).val(); $.ajax({ ...

Firefox Misplacement: Erroneously Shifting Element Locations

I have recently completed designing and developing a new website, which you can visit here. However, I am currently facing an issue with the positioning of the "About Us" image when accessing the site on either a tablet or a mobile device. Strangely enou ...

Utilizing group by date feature in Angular ag-Grid

I'm working on setting up an ag-grid with columns for date, time, and location. Below is the code snippet: list.component.ts columnDefs: any = [{ headerName: 'Date', field: 'date', valueFormatter: (data: any) => { ...

Experience seamless one-to-many broadcasting with WebRTC/Kurento, featuring server-side recording capabilities

I am currently exploring Kurento to determine if it fits my needs. I am interested in developing a mobile application that can record and stream video to a server in real-time, with the server saving the video on its file system as it is being transmitted. ...

Mixing CSS layers

Applying this particular css: .addProblemClass{ width:300px; height:300px; /*width:25%; height:40%;*/ border:solid 1px #000000; margin: 5px; background-color:#FFFFFF; padding:5px; opacity:0.9;/*For chrome and mozilla*/ ...

Error: Unable to locate module 'react-calendar-heatmap'

After successfully creating a component that functioned flawlessly in my local application, I encountered an error when attempting to integrate it with npm: ./src/App.js Module not found: Can't resolve 'heatmap-calendar-react' in 'C:& ...

After my jQuery functions are executed, my CSS rules are loaded

Struggling with the communication between CSS and jQuery, I find myself torn. In CSS, my rules are often very specific, like this: div#container > div#contentLeft { //Code here } However, when I add jQuery to spice up my site, the CSS rules seem to ...

What is the best way to recreate this grid-like design using CSS?

By utilizing clever techniques such as margins and outlines, I was able to design the following. If you'd like to take a look at the live website, it can be found at The CSS code that I used is as follows: body { margin: auto; max-width: 736px } s ...

When Components in Vue are called in a Single File, certain elements may not be displaying as expected

I have just finished creating my components using Vue 2, Vuetify, and Vue cli - 4.5.15. I attempted to combine them into a Single Vue file but encountered issues with the components not displaying <v-icons>, <v-textfield>, and some other elemen ...

Bringing together Events and Promises in JS/Node is the key to seamless programming

I am exploring the most effective approach to incorporating events (such as Node's EventEmitter) and promises within a single system. The concept of decoupled components communicating loosely through a central event hub intrigues me. When one componen ...

Provide users with the option to select a specific destination for saving their file

In the midst of my spring MVC project, I find myself in need of implementing a file path chooser for users. The goal is to allow users to select a specific location where they can save their files, such as C:\testlocation\sublocation... Despite r ...

Implementing Multiple Shared Footers in Rails 3

Looking for a solution to display unique footers in Rails? For instance, displaying one footer on the homepage and another footer on the rest of the site. Has anyone successfully implemented this before? I was considering using an if statement based on th ...

Highlighting the current menu item by comparing the URL and ID

Looking to make my navigation menu items active based on URL and ID, rather than href. None of the suggested solutions (like this one, this one, or this one) have worked for me. This is how my Navigation is designed: <nav id="PageNavigation"& ...

Set a variable to a specific cell within a table

I've been attempting to insert images into a table and have had success so far - clicking cycles through the available options. However, I've encountered an issue where the counter is not cell-specific but rather global. Is there a way to create ...

Determine the size of a file in either megabytes or kiloby

I need to determine the size of a file in either megabytes if the value is greater than 1024 or kilobytes if less than 1024. $(document).ready(function() { $('input[type="file"]').change(function(event) { var _size = this.files[0].si ...

Updating a specific row in a table using PHP and SQL

I am having an issue with updating rows in a table that pulls information from a database. Each row has an input tag labeled COMPLETE, which the user can click on to update the database by changing a boolean field from 0 to 1. However, I am facing a prob ...

How can I trigger a row click event while a TextInput is in focus? (react-native)

Whenever I tap on a ListView item, the TouchableOpacity functions properly. However, when a TextInput is in focus, it requires two taps for it to work - the first tap only removes the focus from the TextInput. Is there a way to make it work without havin ...

Adjust the width of your table content to perfectly fit within the designated width by utilizing the CSS property "table width:

Example of a table <table> <tr> <td>Name</td> <td>John</td> <td>Age</td> <td>25</td> <td>Job Title</td> <td>Software Engineer ...

Tips for ensuring a div stays centered while resizing the window

When I resize the window, the div tab on the right side of the screen moves to the bottom right. How can I make it stay in the middle regardless of screen size? I've tried using margin-left:auto and margin-right:auto but it didn't work. Changing ...

adjusting the dimensions of images to ensure uniformity in size

After many attempts, I am still struggling to resize my image. Can anyone offer assistance? I want all images to have the same dimensions. Here is the HTML code: <link href='https://fonts.googleapis.com/css?family=Indie+Flower' rel='sty ...