Changing an image depending on the visibility of another image can be accomplished by using JavaScript to

On a webpage, I have a logo that I want to change based on the visibility of another image. The other image is part of a list that fades between images and repeats.

The goal is for Logo1 to display when a specific image is shown, and then switch back to Logo2 for all other images.

Below is the code I am currently using that is not functioning as expected:

document.getElementById("featureImages").addEventListener("change", ChangeLogo3);
                    
function ChangeLogo3() {
    var x = document.getElementById("chosenBy");
        if (window.getComputedStyle(x).display === "none") {
            document.getElementById("homeLogo").src = img.src.replace("/images/defaultImages/Logo1.png", "/images/defaultImages/Logo2.png");
        }
}

'featureImages' is the ID of the Div containing all the fading images. None of them have individual IDs except for 'chosenBy', which the event listener should be listening for to become 'display: none'.

Each image in the list is coded like this:

<div style="z-index: 17; position: absolute; display: none; opacity: 0.9999;">
<a target="_self" title="title" href="http://www.example.com">
<img class="image" height="450px" width="980px" alt="Alternative" src="/images/sliderImages/header1.jpg"/></a>
</div>

There are around 17 images in total, each with different content. There is also JavaScript code to enable fading transitions between these images.

Answer №1

In a helpful comment, Louys Patrice Bessette pointed out that the change event won't trigger for the image wrap in question. The logic for adjusting image opacity needs to be placed within that specific part of the code. Using jQuery, you can check if an element is hidden like so:

   const x = $('#chosen_by')
   if(x.is(':hidden')) {
     $('#homeLogo').attr('src', '/images/defaultImages/Logo2.png')
   }

Additionally, it's important to verify absolute/relative URLs in your code to ensure they accurately point to the intended images. Furthermore, upon reviewing your JS code snippet, I observed that you are selecting elements by ID without corresponding ID attributes in your 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

Typescript generates a warning when utilizing JSON objects in conjunction with data types

Attempting to access the attributes of a JSON object within a hook using bracket notation. Although it seems to be functioning correctly, TypeScript continues to throw this warning: Property 'github' does not exist on type 'PropertyValues& ...

"Troubleshooting the malfunctioning of the jQuery submit function in Begin

There is a partial view called contactaddpopup.cshtml which contains the following form for adding a contact: @using (Html.BeginForm("Validation", "Contacts", FormMethod.Post, new { id = "myForm"})) ...

prohibit magnifying glass from appearing on iOS 9 in a meteor cordova application

https://i.sstatic.net/jQOpS.jpg I have been experimenting with various solutions to get rid of the intrusive magnifying glass using CSS. Interestingly, on my iOS9 meteor cordova build, it still briefly appears and then disappears after a few milliseconds. ...

Attempting to transfer a username String from the client to the server using React and Typescript

I am working on a project where I need to send the username of a logged-in user from the Client to the Server as a string. Currently, I am able to successfully send an image file, but now I also need to send a string along with it. What I aim to do is repl ...

Exploring AngularJS: Extracting Data Using the GET Method

Here is my Controller: angular.module('apartmentCtrl', []) .controller('ApartmentController', function ($scope, $http, Apartment) { $scope.loading = true; $scope.myLocation = ''; Apartment.get($scope.myLocation ...

Creating an embedded link to a website that adjusts to different screen sizes while also dynamically changing

I've been developing a personalized site builder that includes an iframe for previewing responsive websites. In order to make the site 'zoomed out' and not appear in mobile size, I'm utilizing CSS scale and transform origin as shown bel ...

Updating an event listener during the $(document).ready() function and handling ajax requests

In my Rails 4 application, there is an event listener that I have repeated in three different places. The event listener looks like this: $('.remove-tag-button').on('click', function () { $.ajax({ type: "POST", data: {i ...

What steps should I take in order to modify the color of my header background?

I'm facing an issue with changing the background color of the header along the navigation menu using HTML/CSS. It seems that my current code is not working as expected. I believe that the header selector should contain my topnav section, but changing ...

Error message: "The URL retrieved from the JSON data is being displayed as undefined in a

I encountered an issue while following this tutorial. I am attempting to load an image from a JSON-formatted URL. Despite following the tutorial correctly, I am unable to get the image to load. Upon using react-tools, I noticed that the url field is displ ...

How can we make sure a checkbox is only checked when there is corresponding data inputted?

I am facing an issue with my ng-repeater, where each row contains a checkbox and 3 text fields. I want to ensure that the user can only select the checkbox if they have entered data in all 3 text fields. If the user tries to select the checkbox without ent ...

Using a Firebase forEach loop to push items into an array and returning the completed array

At the moment, I am facing difficulties with nodejs (v6.14.0) because I made a post request that needs to complete some tasks before returning a valid response. The issue is that I receive an empty array as a response initially (the response becomes valid ...

Transform a personalized Google map into a visual representation

I am facing a challenge with the following issue. I have implemented a custom overlay on a Google map and it works perfectly in the browser. However, my dilemma arises when I need to display an image with a marker for each point of interest on a different ...

Display images that have been uploaded on the page either as a grid view or a list of

In my Reactjs application, I am uploading multiple images to Azure Blob Storage. On a page, there is a button to open the upload form. Once the upload completes, I want to display all these images on the same page. Since the images have different aspect ...

Toggle visibility (individually duplicating) one thousand sentences while maintaining optimal browser and host performance

I have created JQuery scripts to toggle the display of future sentences on my page (approximately 1000 duplicates) and I am seeking the most efficient method (for browser/host) to accomplish this task. Here are two functional solutions that I have already ...

Link not functioning correctly on mobile-responsive CSS

Having some trouble with hyperlinks over a background image. The hyperlinks work fine normally, but in mobile view, nothing happens when clicking on them. Below is the code for the hyperlinks. You can also visit the site and see that the "Post a Project" ...

Retrieve the entity object and convert it into JSON format using json_encode

I have an array that looks something like this: $general_informations['company'] = $company_db In this array, $company_db is an Entity object with properties, such as: $city = $company_db->getCity(); After using json_encode(), I want to re ...

Update header component dynamically upon successful login with Angular 11

I am currently using Angular 11 and facing an issue with displaying the username in the header component. The header loads before the login component, which results in the user data being stored in local storage only after the login component is loaded. As ...

Implementing Material UI Slider component to update state upon mouse release, enabling real-time sliding functionality

Is there a way to update the new state only upon mouse release for a Material UI slider, while still allowing real-time tracking of the slide? Material UI offers two events: onChange and onChangeCommitted. The latter gives the desired end result, but the s ...

Total aggregate for a variety of Sliders (Bootstrap 4 & jQuery)

I am currently working on a web page that involves implementing 3 sliders. The total of all three sliders should always be limited to 100%. This project utilizes the Bootstrap 4 framework and jQuery 3.6.2 Here are my current challenges: The combined valu ...

Having trouble formatting JSON data in a jQuery datatable with accurate information

Currently, I am diving into the world of jQuery tables specifically for a report that I am working on. Despite successfully receiving the API response, I am facing challenges in binding it to the jQuery datatable. I have searched through various questions ...