Can you identify the issue within this code that combines HTML5, CSS, and JavaScript?

I'm attempting to create a feature where, upon clicking a button, a hidden div will be revealed. However, my current implementation doesn't seem to be working.


function reload(){
    window.reload();
}
function vis(x,z,a){
    var xpar = document.getElementById(x);
    var zpar = document.getElementById(z);
    var apar = document.getElementById(a);
    xpar.style.display = "block";
    zpar.style.display = "block";
    apar.style.display = "block";
}

#ap{
    text-decoration: none;
    color: darkred;
    font-size: 20px;
    font-family: monospace;
    font-weight: bold;
    text-align: center;
    display: block;
    cursor: default;
}
#ap:active{
    color: red;
}
#tgt, #tgt li{
    display: none;
    color: white;
    background-color: blue;
    text-decoration: overline;
    text-decoration-color: black;
    width: 100px;
    height: 70px;
}
#ap button{
    background: inherit;
    outline: none;
    color: white;
    border: none;
    cursor: pointer;
}
#ap button:active{
    outline: none;
    border: none;
    cursor: pointer;
}
<!DOCTYPE html>
<html lang="en-US" style="background-color: black;">

<body>
    <a id="ap" href="#" onclick="reload()"><button onclick="vis('login','sign','shop')">Click To Open!</button></a>
    <div id="tgt">
    <ul style="list-style-type: none; overflow: hidden; display: inline;">
        <li id="login" style="overflow: hidden; display: inline;">Login</li>
        <li id="sign" style="overflow: hidden; display: inline;">Sign In</li>
        <li id="shop" style="overflow: hidden; display: inline;">Shop</li>
        </ul>
    </div>
</body>
</html>

I'm working in Brackets, so if you notice any errors like syntax issues, please leave your feedback in the comments.

I'm experimenting with JS, CSS, and HTML with limited knowledge, so don't expect me to grasp complex concepts like arrays.

Edit: After trying various solutions, I finally found one that works. However, when I apply it to my code, it still doesn't function correctly.

<!DOCTYPE html>
<html lang="en-US" style="background-color: black;">
<head>
    <title>Hidden List</title>
    <style>
        ul li{
            overflow: hidden;
        }
        .liul{
            display: none;
            color: white;
            font-size: 20px;
            font-family: sans-serif;
            font-weight: bolder;
            text-align: left;
            margin-bottom: 10px;
        }
        .liul ul li{
            background: red;
            width: 60px;
        }
        #shop{
            position: absolute;
            top: 28px;
            left: 15px;
        }
        #home{
            position: absolute;
            top: 50px;
            left: 15px;
        }
        #about{
            position: absolute;
            top: 70px;
            left: 15px;
        }
    </style>
</head>
<body>
    <button class="btn">Click Me!</button>
    <div class="liul">
    <ul style="list-style-type: none; overflow: hidden;">
        <li id="shop">Shop</li>
        <li id="home">Home</li>
        <li id="about">About</li>
        </ul>
    </div>
    <script>
    function reload(){
        window.reload();
    }
    let trg = document.querySelector("div");
    let btn = document.querySelector("button");
    trg.style.display = "none";
    btn.addEventListener('click', ()=>{
        if(trg.style.display === "none"){
            trg.style.display = "block";
        } else {
            trg.style.display = "none";
        }
    });
            
    }
    </script>
</body>
</html>

Edit 2: I managed to fix the errors, they were just some minor syntax issues. Thank you! :)

Answer №1

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Document</title>
    <style>
        body{
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }
        div{
            height: 250px;
            width: 250px;
            background-color: black;
            color: white;
            text-align: center;
            margin-top: 20px;
        }
    </style>
</head>
<body>
    <button>ClickMe</button>
    <div>
        <h1>Greetings</h1>
    </div>
    <script>
        let btn = document.querySelector('button');
        let div = document.querySelector('div');

        div.style.display = 'none';
        btn.addEventListener('click', ()=>{
            if(div.style.display ==='none'){
                div.style.display = 'block';
            }else{
                div.style.display = 'none';
            }
        });
    </script>
</body>
</html>

The idea is straightforward - the hidden div will be revealed when clicked, and it will disappear when double-clicked.

<head>
    <title>Document</title>
    <style>
        body{
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }
        div{
            height: 250px;
            width: 250px;
            background-color: black;
            color: white;
            text-align: center;
            margin-top: 20px;
        }
    </style>
</head>
<body>
    <button>ClickMe</button>
    <div>
        <h1>Greetings</h1>
    </div>
    <script>
        let btn = document.querySelector('button');
        let div = document.querySelector('div');

        div.style.display = 'none';
        btn.addEventListener('click', ()=>{
            if(div.style.display ==='none'){
                div.style.display = 'block';
            }else{
                div.style.display = 'none';
            }
        });
    </script>
</body>

Answer №2

Take a look at this code snippet:

        function showElements(element1, element2, element3){
            
            var el1 = document.getElementById(element1);
            var el2 = document.getElementById(element2);
            var el3 = document.getElementById(element3);
            el1.style.display = "block";
            el2.style.display = "block";
            el3.style.display = "block";
        }
 #ap{
            text-decoration: none;
            color: darkred;
            font-size: 20px;
            font-family: monospace;
            font-weight: bold;
            text-align: center;
            display: block;
            cursor: default;
        }
        #ap:active{
            color: red;
        }
        #tgt li{
            display: none;
            color: white;
            background-color: blue;
            text-decoration: overline;
            text-decoration-color: black;
            width: 100px;
            height: 70px;
        }
        #ap button{
            background: inherit;
            outline: none;
            color: white;
            border: none;
            cursor: pointer;
        }
        #ap button:active{
            outline: none;
            border: none;
            cursor: pointer;
        }
<!DOCTYPE html>
<html lang="en-US" style="background-color: black;">

<body>
    <a id="ap" href="#"><button onclick="showElements('login','sign','shop')">Click To Open!</button></a>
    <div id="tgt">
    <ul style="list-style-type: none; overflow: hidden;">
        <li id="login" style="overflow: hidden;">Login</li>
        <li id="sign" style="overflow: hidden; ">Sign In</li>
        <li id="shop" style="overflow: hidden;">Shop</li>
        </ul>
    </div>
</body>
</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

Setting a default image for the img setAttribute: A step-by-step guide

My code involves creating an img element using JavaScript: var photo = document.createElement("img"); This element is populated with photos whose names are stored in an array of objects. The images are loaded like this: photo.setAttribute('src&apos ...

Execute the function when the form is submitted and show the total of the two values

I am currently working on a straightforward custom module for Drupal 7. This module takes two numeric values and upon submission, displays the sum of these values on the same page. While I have the form set up, it is not yet complete. I am facing some dif ...

Automatic width table design

I am trying to position the "User ID" label closer to its value by aligning it to the right side, as shown in the picture below. I have attempted using additional colspan's but haven't been successful. The User ID can contain anywhere from 1 to 9 ...

Mastering all changes to the 'src' attribute in the DOM

I am currently coding in Javascript and trying to implement a feature that monitors new 'script' elements and blocks specific sources. I have experimented with using MutationObserver and __defineSetter__, both of which can monitor changes to the ...

Eliminating table rows using jQuery

In order to remove table rows from the table below: <div id="tabel"> <table class="tg"> <thead> <tr><td colspan=3>How can I delete rows from a table using jQuery</td></tr> <t ...

What could be causing my Ajax function to malfunction?

I've been attempting to incorporate a form that sends two javascript variables to a php script and displays the result in a new Div on the same page using an ajax function. Unfortunately, it's not functioning as expected. Here is the code snippe ...

The style of CSS remains constant within the JSP file

Having trouble updating styles on a JSP page using CSS. Here is the code snippet for linking: <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/Profile.css" /> Initially, the styling wo ...

What is the best way to locate and extract the content of an HTML element using XPath in Selenium with VBA?

I am currently seeking a solution to extract the content of an element with the name "data-testid" from a website. This specific element appears approximately 35 times within various contexts in the HTML code. The particular element I am interested in look ...

Creating an Angular 7 Template using HTML CSS and Javascript

I recently acquired a template that comes with HTML, CSS, and Javascript files for a static webpage. My goal is to integrate these existing files into my Angular project so I can link it with a Nodejs application to create a full-stack webpage with backend ...

hasOwnProperty function yields no results

I need help displaying a table from JSON data. Can someone assist me with the code below? <th> { x.amountForQuantity.filter((remaining) => { return remaining.hasOwnProperty(cost.key); })[cost.key] } </th> ...

Understanding the process of parsing an array in form-data in Node.js

https://i.stack.imgur.com/FPhX1.png I'm currently facing an issue while trying to read the image above in Node.js. I am using Express.js and have attempted to debug req.body, but it is returning an empty object {}. Even though I am using app.use(bod ...

Discovering the process of extracting a date from the Date Picker component and displaying it in a table using Material-UI in ReactJS

I am using the Date Picker component from Material-UI for React JS to display selected dates in a table. However, I am encountering an error when trying to show the date object in a table row. Can anyone provide guidance on how to achieve this? import ...

Using jQuery DataTable to fetch JSON data upon Document Loaded

I am struggling to implement a PHP script that returns JSON data to populate a DataTable. The PHP script 'api/exceptions_all.php' is as follows: <?php $select = "SELECT '', [FOR_PARTNER], [FOR_NAME] FROM [brokerage].[dbo].[for_ex ...

Encountering an unexpected token 'Indent' while working with Jade in Eclipse

Currently, I am delving into the world of node, angular, javascript, express, and jade. Transitioning from a purely .net based coding environment has presented me with quite the learning curve. While following a tutorial, I encountered a roadblock at step ...

Personal Information Management

After making a request for stormpath user custom data, I used the following code: res.render('home', { title: 'home', user: req.user.customData, }); Instead of receiving a JSON object of custom data as expected, a URL ('') ...

Using Object Values and Subvalues to Assign Element Attributes in jQuery

I have a weekly schedule that I update regularly using a static table layout: <table> <tr class="live gm fsp"> <td>Oct. 7</td> <td>12:30 pm</td> <td class="prog">Show 1</td> <td>Team ...

Set datatables to perform regex searches that specifically target the beginning of text

Here is my JavaScript code for using datatables, I want to have the search function only filter results that start with the specified keyword. For example, if I have [hello, hello_all, all_hello] and my search term is "hel", I should only get [hello, hel ...

Customizing a thumbnail script to dynamically resize and display images according to their size properties

I am attempting to modify a simple JavaScript script that enlarges an image from a thumbnail whenever it is clicked. The issue I am facing is that the enlarged image is displayed based on a fixed width size. I want the enlarged image to be displayed accord ...

Achieve Dual Functionality with Vue.JS Button within a Parent Element

My parent component structure looks like this: <template> <b-container> <b-modal id="uw-qb-add-item-modal" ref="uw-qb-add-item-modal" title="Enter Item Number" @ok="handleNewItem"> <f ...

The File is not being successfully sent to the controller in MVC due to AJAX returning an undefined value

I recently created an AJAXUpload method within a cshtml file in my C# MVC project: function AjaxUpload(url, method, data, successFunction, errorFunction, skipErrorDlg) { $.ajax({ contentType: false, processData: false, url: url ...