When attempting to click on my subtopics using jQuery, they do not appear as expected

$(document).ready(function () {
    $("#subTopics").hide();
    $("#mainTopics").click(function () {
        $("#subTopics").show("slow");
        
    });
});
body
{
    margin: 0;
}

li, a{
    text-decoration: none;
    list-style-type: none;
    text-decoration-line: none;
    color: black;
}


#main-menu {
    position: relative;
}

#main-menu ul {
    margin: 0;
    padding: 0;
}

#main-menu li {
    display: inline-block;
}

#main-menu a {
    display: block;
    width: 100px;
    padding: 10px;
    border: 1px solid;
    text-align: center;
}


#subTopics {
    position: absolute;
    margin-top: 10px;
    width: 100%;
    left: 0;
}

#subTopics ul {
    margin: 0;
    padding: 0;
}

#subTopics li {
    display: block;
}

#subTopics a {
    text-align: left;
}


#column1, #column2, #column3, .columns {
    position: relative;
    float: left;
    left: 125px;
    margin: 0px 5px 0px 0px;
}


#main-menu li:hover {
    text-decoration: underline;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>
    <link rel="stylesheet" type="text/css" href="index.css" />
</head>
<body>
    <header>

    </header>
    <nav>
        <div id="main-menu">
            <ul>
                <li><a href="">Logo</a></li>
                <li><a href="">Home</a></li>
                <li><a href="" id="mainTopics">Topics</a>
                    <div id="subTopics">
                        <div id="column1" class="columns">
                            <ul>
                                <li><a href="">example1</a></li>
                                <li><a href="">example2</a></li>
                                <li><a href="">example3</a></li>
                                <li><a href="">example4</a></li>
                                <li><a href="">example5</a></li>
                                <li><a href="">example6</a></li>
                                <li><a href="">example7</a></li>
                                <li><a href="">example8</a></li>
                                <li><a href="">example9</a></li>
                                <li><a href="">example10</a></li>
                            </ul>
                        </div>

                        <div id="column2" class="columns">
                            <ul>
                                <li><a href="">example11</a></li>
                                <li><a href="">example12</a></li>
                                <li><a href="">example13</a></li>
                                <li><a href="">example14</a></li>
                                <li><a href="">example15</a></li>
                                <li><a href="">example16</a></li>
                                <li><a href="">example17</a></li>
                                <li><a href="">example18</a></li>
                                <li><a href="">example19</a></li>
                                <li><a href="">example20</a></li>
                            </ul>
                        </div>

                        <div id="column3" class="columns">
                            <ul>
                                <li><a href="">example21</a></li>
                                <li><a href="">example22</a></li>
                                <li><a href="">example23</a></li>
                            </ul>
                        </div>
                    </div>
                </li>
                <li><a href="">Jobs</a></li>
                <li><a href="">Markets</a></li>
            </ul>
        </div>
    </nav>
    <script src="jquery.js"></script>
    <script src="index.js"></script>
    <script>

    </script>
</body>
</html>

Hello, I am experiencing an issue where when I click on the element 'subtopics', it does not show up as intended. Additionally, upon refreshing the page, there seems to be a flickering effect where the subtopics appear and disappear quickly. Can any experienced web developer help me troubleshoot this problem? Thank you in advance for your assistance. (Apologies for any language barriers) ****My main goal is to have the 'subTopics' dropdown or show up upon clicking, rather than hovering over it.****

Answer №1

HTML

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title></title>
<link rel="stylesheet" type="text/css" href="index.css" />
</head>
<body>
<header>

</header>
<nav>
    <div id="main-menu">
        <ul>
            <li><a href="">Logo</a></li>
            <li><a href="">Home</a></li>
            <li><a href="" id="mainTopics">Topics</a>
                <div id="subTopics" class="classSubTopics">
                    <div id="column1" class="columns">
                        <ul>
                            <li><a href="">example1</a></li>
                            <li><a href="">example2</a></li>
                            <li><a href="">example3</a></li>
                            <li><a href="">example4</a></li>
                            <li><a href="">example5</a></li>
                            <li><a href="">example6</a></li>
                            <li><a href="">example7</a></li>
                            <li><a href="">example8</a></li>
                            <li><a href="">example9</a></li>
                            <li><a href="">example10</a></li>
                        </ul>
                    </div>

                    <div id="column2" class="columns">
                        <ul>
                            <li><a href="">example11</a></li>
                            <li><a href="">example12</a></li>
                            <li><a href="">example13</a></li>
                            <li><a href="">example14</a></li>
                            <li><a href="">example15</a></li>
                            <li><a href="">example16</a></li>
                            <li><a href="">example17</a></li>
                            <li><a href="">example18</a></li>
                            <li><a href="">example19</a></li>
                            <li><a href="">example20</a></li>
                        </ul>
                    </div>

                    <div id="column3" class="columns">
                        <ul>
                            <li><a href="">example21</a></li>
                            <li><a href="">example22</a></li>
                            <li><a href="">example23</a></li>
                        </ul>
                    </div>
                </div>
            </li>
            <li><a href="">Jobs</a></li>
            <li><a href="">Markets</a></li>
        </ul>
    </div>
</nav>
<script src="jquery.js"></script>
<script src="index.js"></script>
<script>

</script>
</body>
</html>

CSS

#subTopics {
position: absolute;
display: none;
margin-top: 10px;
width: 100%;
left: 0;
}

jQuery

$(document).ready(function () {
$("#mainTopics").click(function (event) {
    $(".classSubTopics").hide();
    event.preventDefault()
    $("#subTopics").show("slow");
});

});

Answer №2

Your website can function perfectly fine without relying on jQuery. You have the option to achieve the desired effects using only css.

body
{
    margin: 0;
}

li, a{
    text-decoration: none;
    list-style-type: none;
    text-decoration-line: none;
    color: black;
}


#main-menu {
    position: relative;
}

#main-menu ul {
    margin: 0;
    padding: 0;
}
...
... (additional CSS code)
...
<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>
    <link rel="stylesheet" type="text/css" href="index.css" />
</head>
<body>
    <header>

    </header>
    <nav>
        <div id="main-menu">
            <ul>
                <li><a href="">Logo</a></li>
                <li><a href="">Home</a></li>
                ... (additional HTML code) ...
            </ul>
        </div>
    </nav>
    <script src="jquery.js"></script>
    <script src="index.js"></script>
    <script>

    </script>
</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

Selecting CSS Properties with jQuery

I am trying to make an image change color to blue and also change the background color to blue when it is clicked inside a div. However, my code doesn't seem to be working as expected. Is there a more efficient way to apply CSS to elements? FIDDLE v ...

Why is it necessary in JavaScript to reset the function's prototype after resetting the function prototype constructor as well?

Code is often written in the following manner: function G() {}; var item = {...} G.prototype = item; G.prototype.constructor = G // What is the purpose of this line? Why do we need to include G.prototype = item before resetting the prototype? What exact ...

Having trouble aligning page in the center with flexbox and styled components

Hey there, I'm having some trouble figuring out how to center my page using flexbox within styled components in a Next.js app. Any suggestions or tips? Let me share with you my Blog Component import ReactMarkdown from 'react-markdown' impor ...

The styles for the React calendar are not being properly applied to the calendar component due to CSS overriding

Having trouble overriding the default Calendar.css file in NextJS while creating a calendar component. Even after adding my own custom styles, they aren't being applied. Deleting the css file contents doesn't change the format either. Only when c ...

Guide on making API calls in AngularJS using query strings

I am new to learning about AngularJS and recently came across a helpful article on connecting to an API and using its data in our app. The article specifically focuses on displaying weather information with AngularJS. The only downside is that the weather ...

What is the best way to smoothly reveal images one by one?

My goal is to smoothly slide three images inside a div, one by one. However, I'm facing an issue where the images stack on top of each other and end up stuck in their original positions. Here is the code snippet I've been working with: <div ...

A step-by-step guide to displaying an image preview when hovering over a cell in a

How can I display an image when hovering over a <td> in a datatable after clicking a button to fill the table with data? Here is my code: <script> if($.fn.dataTable.isDataTable( '#example' )){ var table = $('#example'). ...

Anchor link leading to an incorrect location

Sorry for the potentially silly question, but I'm struggling to figure out what's happening here. I'm currently working on a website and trying to create an anchor link at . I've placed the anchor just before in this code: <a name ...

Seemingly the Tailwind Styles are failing to take effect in my Next.js Project

While following a Next.js tutorial project, I ran into an issue where my project seemed to be way off. It appeared that the tailwind styles were not being applied for some reason, even after tweaking the 'tailwind.config.js' file without success. ...

I'm attempting to retrieve information from my vuex store, however, encountering an error in the process

I've encountered an issue with vuex getters while working on my project. I have a route that showcases all users, and upon visiting this route, the AllUsers.vue component is displayed. Within this component, I'm utilizing the UsersList.vue compo ...

Troubleshooting: 404 Error When Trying to Send Email with AJAX in Wordpress

In the process of creating a unique theme, I encountered an interesting challenge on my contact page. I wanted to implement an AJAX function that would allow me to send emails directly from the page itself. After conducting some research, I managed to find ...

"Exploring the possibilities of Ajax in conjunction with Sol

I recently completed a tutorial on Ajax Solr and followed the instructions in step one. Below is the code I wrote: header.php: <script type="text/javascript" src="static/js/ajax-solr/core/Core.js"></script> <script type="text/javascript" s ...

Looping through the json resulted in receiving a null value

When working with typescript/javascript, I encountered an issue while trying to fetch the 'statute' from a data object: {_id: "31ad2", x: 21.29, y: -157.81, law: "290-11",....} I attempted to assign data.law to a variable, but received a typeer ...

The appearance of the page varies between Ubuntu and Windows 7 when viewed on Firefox 31

This situation is completely new to me: The page I designed appears differently on Firefox 31 in Ubuntu (correct): compared to Windows 7 (wrong): I was able to replicate this issue on another Windows 7 machine using FF 31. My HTML and CSS both validate w ...

How to Override Certificate Expiry in JavaScript

Is there a way to bypass security for an expired certificate when making an Https post request in Javascript? I have been successful in doing this with C# and Java, but unsure about how to proceed in JavaScript. function post() { var xmlhttp; xmlhtt ...

In a React application, the input field unexpectedly loses focus after typing just one character

Has anyone encountered an issue with a dropdown menu causing input field focus to be lost after typing one character? I've attempted setting unique keys for each field, but it hasn't resolved the problem. For reference, here is the link to the p ...

A guide on triggering a new chart to appear beside the adjacent <div> when a bar is clicked in a highchart

I'm a beginner with Highcharts and I have a requirement for two charts (let's call them Chart A and Chart B). Creating one chart is straightforward. What I need is, upon clicking on a bar in Chart A, a new chart (Chart B) should open next to the ...

Modifying the attributes/properties within a class of a bootstrap button

Here is the code that I have: <b-button id="search-button" size="md" class="mt-1 w-100" type="submit" @click="someEvent()" >Example </b-button If we imagine calling someEvent() and wanting to modi ...

Having trouble with request-promise in node.js? It's not functioning as anticipated

Currently utilizing the request-promise node module. Following the documentation closely to ensure correct setup, however encountering the following error: Unhandled rejection StatusCodeError: 400 - "{\n \"error\" : {\n \"s ...

What steps can be taken to solve the JavaScript error provided below?

My objective is to create a new variable called theRightSide that points to the right side div. var theRightSide = document.getElementById("rightSide"); Once all the images are added to the leftSide div, I need to use cloneNode(true) to copy the left ...