JavaScript is malfunctioning following recent modifications

My JavaScript code is designed to change the z-index of an image when the mouse hovers over it. However, I recently made some changes to the dimensions and positioning in the CSS, causing the script to stop working for the images. To see an example, visit and check the Home page where it's not working, and any other page where it is working.

JS

<script>

A=""
function mouseover1() {
document.getElementById(A).style.zIndex="1"
}
function mouseoff1() {
document.getElementById(A).style.zIndex="100"
}
</script>

CSS

#HButton { background-image:url(../DefaultPage/Buttons/Home.gif); background-color:#000; height:14.8%; width:100%; color:#FFF; background-size:100%; position:absolute; z-index:90; left:0px; background-repeat:no-repeat;}

#HButton2 { background-image: url(../DefaultPage/Buttons/HomeP.gif); background-color:#000; height:14.8%; width:100%; color:#FFF; background-size:100%; position:absolute; z-index:89; top:0px; left:0px; background-repeat:no-repeat;}

This is the CSS code that is causing issues


Below is the correct code:

CSS

#HButton { background-image:url(../DefaultPage/Buttons/Home.gif); background-color:#000; float:left; height:60px; width:350px; color:#FFF; background-size:100%; position:relative; z-index:90;}

#HButton2 { background-image: url(../DefaultPage/Buttons/HomeP.gif); background-color:#000; float:left; height:60px; width:350px; color:#FFF; background-size:100%; position:relative; z-index:10; top:-60px;}

Both codes use the same div tag

HTML

<div id="HButton" onmouseover="A='HButton'; mouseover1()" onmouseout="A='HButton'; mouseoff1()">
</div>
<div id="HButton2" onmouseover="A='HButton'; mouseover1()" onmouseout="A='HButton'; mouseoff1()">
</div>

Answer №1

Upon closer inspection, I have identified two discrepancies:

1) Google ads are present on the home page but not AceWorks (although this likely isn't the cause of the problem).
2) The script tag was properly closed on AceWorks but overlooked on the Home page.

Kindly address the second issue.

Answer №2

Did you overlook semicolons? A=""; Using semicolons is crucial in javascript programming An even better solution would be to pass variable A as a parameter to a function

function onMouseOver(A)

Answer №3

Make sure to check your console log for any errors indicating that the functions are not defined.

Alternatively, you can try this method:

Javascript

function highlightElement(element){
    element.style.zIndex = "1";
}

function resetElement(element){
    element.style.zIndex = "100";
}

HTML

<div id="HighlightButton" onmouseover="highlightElement(this)" onmouseout="resetElement(this)"></div>

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

Error: NgFor can only be used to bind to data structures that are iterable, such as Arrays. JSON arrays are

Encountering ERROR Error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables like Arrays. *ngFor="let spec of vehicleSpecs" Tried various solutions and searched extensi ...

Tips for ensuring the validity of data in an AJAX request

When trying to fetch data using the REST method, I utilize an AJAX call. While my work is completed, I encounter an issue with validating the "data" within the AJAX call. How can I accomplish this? If there is no data returned from the specified URL, it ...

How can I transmit both the $_POST and $_FILE variables to a .php file using Ajax?

I am looking to integrate an ajax image uploading system into my website for various reasons. I want the upload process to be seamless without having to refresh the page. While some jquery plugins have worked well for this purpose, I encountered an issue ...

What is the process of creating a parent-child rowspan HTML Table in a Blazor Server Application?

I am currently facing a challenge in my Blazor Server application where I am attempting to present parent-child data with rowspan in an HTML table. The table is being rendered, however, it is not displaying correctly. Here is the JSON data I am working wi ...

I just installed Electron on my Mac using the command 'sudo npm install electron -g', but now I am encountering an error. How can I resolve this issue

When I first attempted to run the command, I encountered 'Permission Denied' errors so I added sudo before the command as suggested. Another recommendation was to install the electron folder at /usr/local/lib/node_modules, but even after reinstal ...

Monitoring individual elements of an array within an Angular service

Is there a way to monitor changes in an array element within a service? Let's consider the following scenario with CartController and ProductListService. Within the ProductListService, data is fetched as follows: /** * Fetch all the products in us ...

Navigating through Sails.Js: A Guide to Implementing Pagination

Looking to implement paginated tables in your application using sails.js, mongodb, and waterline-ORM? Wondering if there is a recommended method for pagination in sails.js? ...

I would like to add a border at the bottom of each item in a ul list

My current focus is on making my website responsive with a breakpoint set at 576px I am aiming to achieve the design shown in this image without any space in the border-bottom and have both elements expand to full width: menu li hover However, I'm c ...

Offspring containers fail to adjust in size relative to their parent container

I am working with a parent div that contains 5 child divs. The parent div does not fill the entire width of the window, but the 5 children share the same space and properly fill their parent. <div class="parent"> <div class="child ...

AngularJS - Leveraging the power of two-way data binding with JSON strings

I am a beginner with AngularJS and I'm currently working on retrieving, parsing, and displaying data from a SOAP web service. So far, I have been able to successfully make calls to a public weather service, capture and display the XML data that is ret ...

Stopped due to an error: TypeError - document.getElementById(...) is not defined

Currently, I am working on developing a commenting system. Everything seems to be functioning well as long as there is at least one existing comment. However, if there are no comments present and an attempt is made to create one, an error message pops up d ...

Angular directive that utilizes a dynamic controller name and then interpolates the controller name

Can anyone assist me with passing controller definitions to the inner directive that is nested within the outer directive? You can refer to this link for an example that may or may not work. Is there a way for Angular to interpret what is being passed ...

Arranging List Items within a Container

What's the best way to align three different elements within a div? The elements are structured in an unordered list: Left, Center, and Right. I attempted to float the right element using float: right, and applied margin: 0 auto with a set width for ...

When navigating to '/blogs/', the index.js file in Next.js will automatically open

I'm working on a project using next.js and I want to ensure that when I visit 'localhost:3000/blogs/', it opens the index.js page. The index.js file is located in the 'blogs' folder of my project. Currently, it does open properly ...

Error: The outcome of the function is not being displayed

I've been encountering an issue with rendering the result of the function renderVerticalCards. Despite confirming that the function is indeed being called through the console, nothing is displayed in the DOM except for the outer divs from outside the ...

Easily done! Using JavaScript to generate a blinking cursor on a table

Working on a project to develop a dynamic version of the classic game Tic-Tac-Toe... However... Every table cell is displaying an irritating flashing cursor, almost like it's behaving as an input field. Any insights into why this is happening...? O ...

Which specific graphing library does GitHub utilize for its Graphs section?

Which graphing library is utilized by GitHub on its Graphs tab? The graphs displayed at https://github.com/USER/REPOSITORY/graphs/commit-activity are visually appealing, detailed, and adaptable. If they are leveraging an open source javascript library fo ...

Can someone provide guidance on iterating through a nested array in d3 and appending child elements accordingly?

Looking at my JSON data, here is an example of what it contains. var data = [ { animal: 'dog', names: [ 'mark', 'cooper', 'pooch' ] }, { animal: 'cat', names: [ 'mary', 'kitty' ] ]; ...

Guide to Angular Directives: Assigning Attributes to innerHTML

Looking to add attributes to the inner HTML myTemplate.html <div class="custom-template"></div> HTML <template></template> directive app.directive('template', ['$compile', function($compile) { retur ...

Setting a variable prior to receiving feedback in Javascript

Uncertain of the appropriate query, but I will attempt to elucidate in my current operational code: var container, loader switch(section){ case 'A': container = $('#list .container') loader = $('#surveylist&apo ...