Switching Logo Image when Menu Button is Clicked

I'm looking to adjust the color of my logo when the menu button is clicked to match the overlay style.

Currently, I managed to switch from a black logo to a white logo. However, when attempting to close the menu, it doesn't revert back to the black logo as intended in my JavaScript function.

Any assistance would be greatly appreciated.

function menuButton(x) {
  x.classList.toggle("change");
}

function toggleNav() {
  var x = document.getElementById("myNav");
  if (x.style.height === "100%") {
    x.style.height = "0%";
  } else {
    x.style.height = "100%";
  }
}

function changeImg() {
  var logo = document.getElementById("logo");
  if (logo.src == "/images/1x/White.png") {
    logo.src = "/images/1x/Asset 1.png";
  } else {
    logo.src = "/images/1x/White.png";
  }
}
/* CSS Styles */

.white {
  color: #fff
}

html,
body {
  margin: 0;
  padding: 0;
}


header {
  /* Header Styles */
}

.container {
  /* Container Styles */
}

header .logo {
  /* Logo Styles */
}

header .menu {
  /* Menu Styles */
}

.bar1,
.bar2,
.bar3 {
  /* Bar Styles */
}

.change .bar1 {
  /* Change Bar Styles */
}

.overlay {
  /* Overlay Styles */
}

.overlayContent {
  /* Overlay Content Styles */
}

.overlay a {
  /* Overlay Link Styles */
}
<link href="https://fonts.googleapis.com/css?family=Raleway" type="text/css" rel="stylesheet">

<!-- HTML Markup -->

<header id="top">

  <div class="container">

    <div class="logo">
      <a href="#top" class="logo"><img id="logo" src="/images/1x/Asset 1.png" alt="Logo"></a>
    </div>

    <div class='menu' onclick="menuButton(this); toggleNav(); changeImg()">
      <div class="bar1"></div>
      <div class="bar2"></div>
      <div class="bar3"></div>
    </div>

    <div class="container">
      <nav id="myNav" class="overlay">
        <div class="overlayContent">
          <a href="#">Item 1</a>
          <a href="#">Item 2</a>
          <a href="#">Item 3</a>
          <a href="#">Item 4</a>
          <a href="#">Item 5</a>
        </div>
      </nav>
    </div>
  </div>
</header>

<div id="showcase">

</div>

https://i.sstatic.net/vAWqo.png

https://i.sstatic.net/gpqdr.png

Answer №1

Ensure you are utilizing the absolute path of the image as the source.

Within the switchImage method, modify your if statement to:

if (logo.getAttribute('src') === '/images/1x/White.png')

By making this adjustment, the function will successfully execute.

Answer №2

It appears there is a minor mistake in the code snippet below:

logo.src = "/images/1x/Asset 1.png";

It's important to avoid using spaces in file names or asset names. Consider replacing the space with an underscore and renaming the image file to Asset_1.png. Then, update the source path in the code to:

logo.src = "/images/1x/Asset_1.png";

Don't forget to update the filename wherever it is used in your HTML or CSS as well.

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

Trigger the mousemove event only after the mouse click event has been activated

I need help with my code. I want an onmousemove event to occur when I click and move the mouse. Can someone assist me please? </head> <body> <img id="myImgId" alt="" src="Chrysa ...

What is the best way to incorporate a website logo: using the <img> tag

As I work on building a practice website, I've noticed that I have an IMG tag for the website logo on every HTML subpage. I'm wondering if it would be more efficient to use a background image in CSS instead of the IMG tag? <div id="logo_w ...

Send the information to MongoDB in the form of an object, utilize it as a query, and then perform

I have a document stored in my mongoDB database. https://i.sstatic.net/2DI2p.png When a user types something into an input field on the frontend, for example 'test', the object passed looks like this: {'countdown': 'test'} ...

Problems arose when attempting to adjust the size of the container function

I've been working on a Joomla 2.5 template that consists of three main sections: top, container, and footer. While trying to set the "container" section to have a minimum height of 100%, I faced various challenges which led me to use jQuery for assist ...

The renderValue property is malfunctioning in the Material-UI native Select component

Whenever I utilize the native prop in the MUI Select component, the renderValue prop fails to function properly. Additionally, if I attempt to assign a custom value to the value prop, it does not display. Take a look at the code snippet below: const [selec ...

Sending Data to a PHP Script

I attempted to use the code below to send values to another PHP page but I am not receiving any output. File: Send.html <form id="form1" action="get.php" method="get"> <input name="search_name" type="text" id="search_name" size="20.5" height="45 ...

Ways to obtain distinct JavaScript key codes for [ and { and ' and "

Greetings all! I'm currently working on a text editor that has the ability to automatically complete brackets and quotes by using jQuery. In order to achieve this, I am utilizing key codes. However, I have encountered an issue where the characters [ a ...

Angular2 does not load Js twice

I specified the path to my JS file in angular.cli. It loaded successfully during the initialization of the Angular app, but when navigating back to the component, it failed to load. Any suggestions on how to fix this issue would be greatly appreciated. Th ...

The click event does not point to the servlet (IDEA, java)

I am facing an issue where the command $.get('MyController1', function (responseText) is not sending to my servlet. I am using ajax and after clicking the button it should send to my servlet ('MyController1' or 'MyController2' ...

Simulating a traditional table scroll function on a window using virtualization

I am currently attempting to incorporate a virtualized table in react using react–virtualized, but I am facing issues with the rendering of the table. I am seeking to understand the root cause for this problem as well as find solutions for other overlapp ...

Discrepancy in inner div presentation between Internet Explorer and Chrome

In my coding dilemma, the "outerDIV" contains an "innerDIV", with Chrome displaying the "innerDIV" at 491px while IE shows it as 425px (matching outerDIV). This discrepancy causes issues in viewing child elements within "innerDIV": Chrome shows the first t ...

Loading components dynamically with axios is a valuable feature

Can this be achieved? There is a spinner component. axios: action() { SPINNER (component) -- activate axios.get('/store', { params: { something } }) .then ((resp) => { SPINNER (component) -- ...

Inspect each chart for information and conceal any that are empty

Currently, I am attempting to analyze my highcharts graphs for data during loading and each redraw. I have successfully implemented the following code: (function (H) { var chartPrototype = H.Chart.prototype; // Display or hide graph based on da ...

Developing a division with an image source based on the selection of a checkbox

My goal is to develop a "change function" that generates a new div for each checked checkbox selection and removes the div when the checkbox is unchecked. These new divs should also display the img src of the selected checkbox. Currently, my JavaScript c ...

Html flipcard malfunctioning and logo overlapping issue detected

Regarding the following code: <html> <style> .img { max-width: 100%; } .Headerstyle { color:Black; transition: transform .2s; text-align: center; margin-top: 80vh; } .Headerstyle:hover { transform: scale(1.5) ...

When the FLASK button is triggered, retrieve data from the FLASK and display it

This is a unique test where I will create a button to retrieve information from a MySQL database. However, it's important to first understand this concept. So here is my custom text within the Flask framework: @app.route('/pythonlogin/home', ...

Removing a single div box causes a ripple effect on another div box, despite their lack of connection

I'm struggling to remove the white box underneath the "features" section on this website (). The structure of the div is quite simple: <div class="donatenew"></div> and here's the CSS: .donatenew { width:100%; background: #f8f ...

Arranging divs with Bootstrap version 4

Is it necessary to specify the order for each screen size breakpoint when displaying 2 divs in different orders based on the screen size? <div class="order-xs-2 order-sm-2 order-md-2 order-lg-1 order-xl-1"> <div class="order-xs-1 order-sm-1 order ...

Implementing Angular $resource to prepopulate a form with data

My goal is to enable CRUD operations so that when a user selects a specific item to edit, the form will automatically populate with the corresponding values for editing. Below is an excerpt of code from the 'storeView.html' file: <tr data-ng ...

Looking for a Hack to Integrate a Music Player into Your Website?

Currently, I am utilizing the Jplayer plugin from JQuery to incorporate an Audio player into my website. I have come across a situation where: If the user is not currently listening to any music while browsing the website, the page can load without any i ...