I continually run into the error message "Uncaught ReferenceError: dropMenu is not defined" and "Uncaught TypeError: Cannot read property 'style' of null."

I'm attempting to create a navigation bar with submenus, but I keep encountering errors such as "dropMenu is not defined" and "Uncaught TypeError: Cannot read property 'style' of null" when hovering over the Paris links. Any thoughts on what might be causing this issue? Here is my code:

<html>
<link type="text/css"  rel="stylesheet" href="vacation.css"/>
<head>
<title>Paris</title>
</head>
<body>
<div id="menus">
<div id ="parismenu">
<a href="#" onmouseover="dropMenu('dropmenu1')">Paris</a>
<div id="dropmenu1" class="dropmenus">
<a href="#">apple</a>
</div>
</div>
<div id ="disneymenu">
<a href="#" onmouseover="dropMenu('dropmenu2')">Paris</a>
<div id="dropmenu2" class="dropmenus">
<a href="#">apple</a>
</div>
</div>
</div>
</body>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
 <script type="text/javascript" src="vacation.js"></script>
  </html>

CSS

div#menus{
margin-left:16px;
 }
 div#menus> div{
 float:left;
 margin:0px 20px;
 }
 div.dropmenus{
 display:none;
 position:absolute;
 top:100px;
 width:120px;
 background:teal;
 z-index:2;
 padding:4px;
 border: blue 3px solid;
 border-top:none;
 border-radius:5px 5px 5px 5px;
 }
 div.dropmenus>a{
 display:block;
 margin:4px;
 padding:7px;
 font-size:14px;
 text-decoration:none;
 background:blue;
 border-radius:3px;
 color:red;
 }
 div#dropmenu1{
 left:24px;
 }
 div#dropmenu2{
 left:116px;
 }

Javascript

 var fade=function(){
 return{
 init:function(id,flag,target){
 this.elem=document.ElementById(id);
 clearInterval(this.elem.si);
 this.target=target ? target : flag ? 100 : 0;
 this.flag = flag||1;
 this.alpha = this.elem.stle.opacity ? parseFloat(this.elem.style.opacity) * 100 : 0;
 this.elem.si=setIntterval(function(){fade.fadep()},20);
 },
 fadep:function(){
 if(this.alpha==this.target){
 clearInterval(this.elem.si);
 }
 else{
 var value=Math.round(this.alpha + ((this.target - this.alpha) * .05)) +(-1 * this.flag);
 this.elem.style.opacity=value/100;
 this.elem.style.filter='alpha(opacity=' + value + ')';
 this.alpha=value
 }}}}();

 var menu= ["dropmenu1","dropmenu2","dropmenu3"];
 function dropMenu(x){
 for(var m in menu){
 if(menu[m] != x){
 document.getElementById(menu[m]).style.display="none";
 }}
 if(document.getElementById(x).style.diplay=="block"){
 fade.init(x,1);
  }
 else{
 fade.init(x,0)}}

Answer №1

The array consists of three unique identifiers, yet your html script displays only the first two.

document.getElementById(menu[m])

If m equals 2, this line will return as undefined.

To fix this issue, simply remove "dropmenu3" from the menu array.

Furthermore, ensure that the following line is corrected:

this.elem=document.ElementById(id);

A more accurate representation would be:

this.elem=document.getElementById(id);

Answer №2

If you prefer to accomplish this task without using jQuery, here is the code required. The original JavaScript had a few typos which I have corrected. Additionally, I modified it so that all elements not being hovered over are instantly hidden.

HTML

<div id="menus">
    <div id="parismenu">
        <a href="#" onmouseover="dropMenu('dropmenu1');">Paris</a>
        <div id="dropmenu1" class="dropmenus">
            <a href="#">apple</a>
        </div>
    </div>
    <div id="disneymenu">
        <a href="#" onmouseover="dropMenu('dropmenu2');">Paris</a>
        <div id="dropmenu2" class="dropmenus">
            <a href="#">apple</a>
        </div>
    </div>
</div>

JAVASCRIPT

var menu = ["dropmenu1", "dropmenu2"];

function dropMenu(x) {
    for (var m in menu) {
        if(menu[m] != x){
            document.getElementById(menu[m]).style.opacity = 0;
            document.getElementById(menu[m]).style.filter = 'alpha(opacity=0)';
        }
    }
    fade.init(x, 1);
}
var fade=function(){
    return{
        init:function(id, flag, target){
            this.elem = document.getElementById(id);
            clearInterval(this.elem.si);
            this.target = target ? target : flag ? 100 : 0;
            this.flag = flag || -1;
            this.alpha = this.elem.style.opacity ? parseFloat(this.elem.style.opacity) * 100 : 0;
            this.elem.si = setInterval(function(){fade.tween()}, 20);
        },
        tween:function(){
            if(this.alpha == this.target){
                clearInterval(this.elem.si);
            }else{
                var value = Math.round(this.alpha + ((this.target - this.alpha) * .05)) + (1 * this.flag);
                this.elem.style.opacity = value / 100;
                this.elem.style.filter = 'alpha(opacity=' + value + ')';
                this.alpha = value
            }
        }
    }
}();

I did not modify your CSS. You can test the code with a working jsfiddle here.

Answer №3

Have you considered utilizing the jQuery fader for this task? I've made some improvements to your JavaScript and HTML. Instead of relying on the element onMouseover event, I've added a listener to that element for the event after the document has loaded.

<!-- HTML -->
<div id="menus">
    <div id="parismenu">
        <a href="#" class="dropmenu" data-menu-id="#dropmenu1">Paris</a>
        <div id="dropmenu1" class="dropmenus">
            <a href="#">apple</a>
        </div>
    </div>
    <div id="disneymenu">
        <a href="#" class="dropmenu" data-menu-id="#dropmenu2">Disney</a>
        <div id="dropmenu2" class="dropmenus">
            <a href="#">apple</a>
        </div>
    </div>
</div>

Next, in the JavaScript:

$(function () {
    $('.dropmenu').on('mouseover', function (event) {
        $($(this).attr('data-menu-id')).fadeIn();
    });
    $('.dropmenu').on('mouseout', function (event) {
        $($(this).attr('data-menu-id')).fadeOut();
    });
});

Your CSS remains unchanged.

Check out this JSFIDDLE link for a demonstration of the working example.

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

Implementing a file download feature in Python when clicking on a hyperlink

I'm attempting to click on the href link below. href="javascript:;" <div class="xlsdownload"> <a id="downloadOCTable" download="data-download.csv" href="javascript:;" onclick=&q ...

What is the best way to designate external dependencies in WebPack that are not imported using '*'?

I need assistance with specifying office-ui-fabric-react as an external dependency in my TypeScript project using Webpack. Currently, I am importing only the modules I require in my project: import { Dialog, DialogType, DialogFooter } from 'office-u ...

Obtain user input and extract the name using jQuery's serialization function

Trying to extract user input from a dynamic form using jquery serialize. The structure of my form is as follows: <form id="lookUpForm"> <input name="q" id="websterInput" /> <button onclick="webster(); return ...

"The combination of Node.js, Express, and Angular is causing a continuous loop in the controller when a route is

Currently, I am utilizing node js alongside Express. Angular js files are being loaded through index.html. The code for app.js is as follows: app.use(bodyParser.json()); // for parsing application/json app.use(bodyParser.urlencoded({ extended: true })); ...

The problem lies in the incorrect rewriting of static resources leading them to be redirected to the

I am currently facing an issue with rewriting URLs. It seems that the problem lies in the redirection of certain URLs to index.php?route=scores, etc. http://www.example.com/scores http://www.example.com/registreren http://www.example.com/login This redir ...

Storing Unique Characters in the Database

Currently, I am utilizing a combination of HTML and PHP for my website, but I have encountered an issue when saving content in a textarea. Whenever I incorporate a different font with special characters like ' and ", it saves as � (a black dia ...

Iterating through an array and displaying information according to the quantity in node.js

Hey everyone, I have a simple task at hand where I am working with the following array: {items:[{upc:a,quantity:2},{upc:b,quantity:3}]} My goal is to transform it into the format shown below: {products:[{barcode:a},{barcode:a},{barcode:b},{barcode:b},{bar ...

Is there a way to dynamically modify a website's default viewport settings in a mobile browser?

When viewing a website in Landscape mode, everything looks good. However, switching to Portrait mode displays the message "Screen size not supported." I decided to test this on my desktop browser and discovered that adjusting the initial-scale:1 to initial ...

Exploring the methods for monitoring multiple UDP ports on a single address in Node.js within a single process

I am currently working on developing a Node.js application to manage a small drone. The SDK provides the following instructions: To establish a connection between the Tello and a PC, Mac, or mobile device, use Wi-Fi. Sending Commands & Receiving Responses ...

The implementation of CSS in one React component has an impact on the functionality of the Ant Design Select component in

I've run into a puzzling issue where importing CSS styles in one React component is impacting the appearance of Ant Design's Select component in another component. Let me break down the scenario: The setup involves two React components, namely P ...

Developing a collection of reusable components in a Javascript bundle for enhanced efficiency

I currently have a backend rendered page (using Django) that I want to enhance by incorporating components from PrimeVue and a markdown editor wrapped as a Vue component. Previously, we utilized some simple animations with jQuery which we included directly ...

Steps for setting the value of a textbox within a bootstrap popover

When a user clicks on an Anchor element, I am displaying a Bootstrap popover using the following JQuery code. Jquery $("[data-toggle=popover]").popover({ trigger: 'click', placement: "top", html: true, ...

Having trouble with .animate() function?

Struggling to animate the position of my background image $(function() { $('#nav1').bind('click',function(event){ $('ul.nav').stop().animate({backgroundPosition: 'right top'}, 1000); }); $(function() { ...

Is the textarea's shape out of the ordinary?

Most textareas are typically rectangular or square in shape, similar to this: However, I am interested in having a custom-shaped textarea, like the example below: Is it feasible to achieve? ...

Creating a countdown clock with JavaScript

I am looking to create a timer using JavaScript that will decrement at different intervals based on the level. For example, if my timer starts at 500, I want it to decrement as follows: 1. The first level timer should decrement by 1 with a slow speed. ...

ng-change not firing when selecting from ng-options list

I am struggling with this code snippet <select ng-model="trabajadores.orderSelected" ng-options="excel for excel in trabajadores.csv.result[1]" ng-change="console.log('changed')"> </select> Despite my best ...

The Node.contains() function in JavaScript does not verify the presence of inner child elements

I have developed a custom DatePicker component for my React app and I am facing an issue with hiding it on outside click, similar to how I handled other components like Select Dropdown. To address this problem, I created a custom hook: export default (ref, ...

The button is disabled once inline CSS is applied to it

I was having issues with the CSS of my buttons being overridden by the user agent stylesheet. To fix this, I used inline CSS to override it. However, a new problem emerged where the buttons became unclickable. Here is the code I am using. Any assistance wo ...

Modify the CSS of one div when hovering over a separate div

I've been working on a simple JavaScript drop-down menu, and it's been functioning correctly except for one issue. When I move the mouse out of the div that shows the drop-down menu, it loses its background color. I want the link to remain active ...

Challenges with Div Height and Border

Hello there! I've run into a height and border issue with a div. Here's the CSS I'm currently working with: .content { background-image: url(../../images/logo-04.png); background-position: left bottom; ...