The Act of Concealing Images - "Error Encountered: Attempting to alter a property that is undefined"

I'm facing a bit of a dilemma and after scouring through numerous forum posts, I still can't seem to pinpoint the issue.

The challenge at hand involves designing a website with a menu button that transforms into an "X" when clicked, toggling back and forth with each click. While the javascript function appears to be functioning, there's an error message saying "Uncaught TypeError: Cannot set property 'display' of undefined" upon trying to change the menu button's display style to "none".

Any insights or suggestions would be greatly appreciated.

Here are snippets of the code:

<img id="menuIconImage" class="menuIcon" src="resources/menu.png" onclick="menuToggle()">
<img id="menuIconImageClose" class="menuIcon" src="resources/close.png" onclick="menuToggle()">

And here's a glimpse of the CSS:

.menuIcon {

    position: fixed;
    left: 33px;
    top: 178px;
    width: 35px;
    height: 35px;
    cursor: pointer;
}

#menuIconImageClose {

    //By default, the "close" image/button is hidden.
    display: none;
}

Lastly, let's take a look at the Javascript snippet:

var menuState = 0;

function menuToggle() {
    "use strict";

    if (menuState === 0) {

        document.getElementById("menuIconImage").stlye.display = "none";
        document.getElementById("menuIconImageClose").style.display = "block";
        menuState = 1;

    } else if (menuState === 1) {

        document.getElementById("menuIconImageClose").style.display = "none";
        document.getElementById("menuIconImage").style.display = "block";
        menuState = 0;      
    }
}   

Answer №1

Notice a misspelling of 'stlye' instead of 'style' in the following line:

document.getElementById("menuIconImage").stlye.display = "none";

The correct line should be:

document.getElementById("menuIconImage").style.display = "none";

To identify the error, I first looked at the error message which indicated that it was not an issue with finding the element (as

document.getElementById("menuIconImage")
worked properly). If the element could not be found, we would receive an error stating:
Uncaught TypeError: Cannot read property 'style' of null
. This helped me narrow down where the problem lay.

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

Using innerHTML to replaceAll also modifies characters within the tags

I created a custom function to emphasize specific strings within a text by surrounding them with span tags and updating the content using the innerHTML property of the targeted element. However, I encountered an issue while working with innerHTML instead ...

Designing a button component with text

Exploring My Demo Page, I am attempting to design a button that will serve as a closure mechanism for modals. The code snippet I experimented with is as follows: x=document.createElement('button'); x.className='superclose'; Referencin ...

PHP AJAX for a Multi-Select Form

Hi there, I'm having some trouble with PHP AJAX to display the result in a select field. My goal is to have the second select field appear after choosing an option in the first one, and then load the third select field when selecting an option in the ...

Strategies for incorporating child element margins within a parent container with overflow scroll

My container has a fixed width causing its children to overflow and require scrolling. Each child element (.box) has a margin-right: 10px. The margin is visible on all elements except the last one, where it is cut off at the edge of the element. I want to ...

After utilizing Geolocation, the input fields in Ionic/Angular JS are failing to update accurately

Currently, I am in the process of developing a new App using Ionic and Angular JS. Specifically, in one of the app tabs, I am utilizing geolocation to populate four fields (street name, street number, latitude, and longitude). Below is the snippet of my c ...

Guide to retrieving the width measurement of a troika-three-text element

Is there a way to retrieve the dimensions (width and height) of a Text object from troika-three-text library? import { Text } from 'troika-three-text' // Instantiate Text object: const myText = new Text() myScene.add(myText) // Customize proper ...

Backbone.js encountered an illegal invocation

http://jsfiddle.net/herrturtur/ExWFH/ If you want to give this a try, follow these steps: Click the plus button. Double click on the newly created name field. Enter information into the fields. Press Enter. The era fields (from and until) will be disp ...

Adjust image size based on the size of the screen

I am facing an issue with two images that are created for a 1024 x 768 resolution but I need to use them in a higher resolution. The challenge is to align these two images so that the circle from the first image falls between the second image. Although I ...

Looking to incorporate nested rules in `JSS` using `CSS`?

Can someone guide me on how to use CSS class-nesting in Material-UI or JSS in general? I have been attempting the following: card: { cardHeader:{ marginTop:"30px" } } ...

Inconsistent Functionality of Bootstrap Popover

I am experiencing an issue with the bootstrap Popover feature. It seems to work inconsistently - sometimes it works and other times it doesn't. I am using it to create a popover that displays a user's information when a visitor hovers over the us ...

Adjust the scroll position when the height of a div is modified

Imagine we have a large div A with a height value and below it are other divs B, C, and more. If the user is viewing divs B or C, and A reduces its height by half, the scrolling position will remain the same. However, divs B and C will move up by that amo ...

Will the bootstrap carousel continue running in the background even if I hide it?

Currently, I have incorporated the twitter bootstrap carousel into my website with the following code: <div id="slider" class="carousel slide"> <!-- Wrapper for slides --> <div class="caro ...

Exploring the latest upgrades in React 18 with a focus on TypeScript integration

I am currently working on a complex TypeScript project with React and recently made the decision to upgrade to the new version of React 18. After running the following commands: npm install react@18 npm install react-dom@18 npm install @types/react-dom@18 ...

Learn how to trigger various servlets by clicking different buttons within an HTML form

I have an HTML form and I need to call two different servlets when two different buttons are clicked. How can I change the form action at runtime? <form> <input type="submit" value="Question Paper" style="height:25px; width:120px; background-colo ...

Modify the Color of Chosen Anchors for Site Categories WITHOUT Using JavaScript

Is there a way to change the color of section anchors in a fixed header when selected without using JavaScript? I'm looking for a CSS-only solution that will change the color of the selected anchor and revert it back to normal when another anchor is c ...

Comparing Fluid-Framework to Signal-R: Understanding the Distinguishing Factors

Query: Recently, Microsoft unveiled the Fluid-Framework on GitHub. What are the steps to replace Signal-R with Microsoft's Fluid-Framework and what are the main differences? Scenario: It appears that Fluid supports collaboration and data synchronizat ...

Setting the ID of an HTML element to an integer within a v-for loop: A step-by-step guide

Currently working on a project using vueJS where I need to generate a specific number of SVG elements using a v-for loop. Each of these elements should have a unique ID derived from the index 'i' in the loop. However, my initial approach resulted ...

React: Introducing the latest router feature post-login

I'm facing an issue with the Router in React. After a successful login, I am changing the type state in Redux from 0 to 1. However, when I try to make a switch in my App file, I encounter an error. Warning: [react-router] You cannot change <Router ...

Using Javascript regex to capture the image name from a CSS file

As I work with JavaScript regex, my goal is to extract the image name and extension as a capture group of CSS properties. Criteria Must start with "url" Followed by brackets Optional quotes inside brackets The location can include path information Must ...

ways to forcefully activate the change function

Here is a jQuery function that successfully sends an AJAX request and receives a response: $('#region_id').on('change', function() { //some AJAX request and response which works great. }); The region_id is a select tag contai ...