Guide on how to set a custom image as the cursor when clicking on an image through javascript

Does anyone know how to use javascript to change the cursor to a custom image by updating the cursor property of the entire page? I'm getting an error in the Chrome console that says:

projetFinalPage1.html:626 Uncaught TypeError: Cannot read property 'style' of null
    at changeCursorGun (projetFinalPage1.html:626)
    at HTMLInputElement.onclick (projetFinalPage1.html:548)
changeCursorGun @ projetFinalPage1.html:626
onclick @ projetFinalPage1.html:548

Here is the code snippet causing the issue:

function changeCursorGun(){
document.getElementById("pageCombatAlien").style.cursor = "url('images/gunCursor')";
}
#pageCombatAlien{
display: none;
cursor: default;
}
<div id="pageCombatAlien">
<div id="backgroundCombatAlien">
<div id="texteCombatAlien">
La voici la fameuse créature que mon détecteur a capté ! Je ne peux pas m'enfuir, car elle me rattarpera.
</div>
<input type="image" src="images/chest.png" onclick="changeCursorGun()" id="chest">
<div id="healthBorder">
<div id="health"></div>
</div>
<input type="button" onclick="healthLoss()" id="boiteClickAlien">
</div>

</div>

Answer №1

There seems to be an error stating that the ElementById you are searching for is null, indicating that it may not exist. Make sure to check your entire HTML code and ensure that PageCombatAlien actually exists.

Alternatively, you can achieve the same effect using only CSS:

Feel free to run the Code Snippet below to see the result:

<div class="test">TEST</div>

<style>
.test {
  background:gray;
  width:200px;
  height:200px;
  cursor:url(http://www.javascriptkit.com/dhtmltutors/cursor-hand.gif), auto;
}
</style>

Answer №2

The issue at hand is indicated by the error message, which states that the PageCombatAlien element cannot be found. Upon reviewing your revised query, it appears that this discrepancy is due to incorrect capitalization. The correct syntax should be

document.getElementById("pageCombatAlien")
instead of PageCombatAlien

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

Selection of multiple listings

Looking for a solution to create a multi list selection area? You can double click on an item in the left list to add it to the selected area. Also, you can double click on an item in the selected area to remove it from the list. <ul id="selection"> ...

Why does serializeArray() function work in Firefox and Chrome, but not in IE(11)?

I am facing an issue with serializing a form that is working perfectly in Chrome and FireFox, but not functioning at all in IE11. The problem seems to be arising from serializeArray() returning an empty array. JSON.stringify($("#enrollmentForm").find( ...

Steps to avoid TypeError: e.target.getAttribute is not a function

My goal is to make the inner code (result) function only when a Validity attribute is present. However, my target lacks said attribute, so I'm looking for a way to use an if statement to prevent the inner code from executing. How can I avoid the Type ...

Timing issue with the animation callback

I have been experimenting with creating a bounce effect on my custom scroller by utilizing the translate3d method for scrolling. I managed to successfully implement it, but now I am facing an issue where if you continuously scroll out of bounds (try double ...

Issue with bookmarking links within a webpage

On the websites I manage, many pages contain localized links in the format of http://www.site.co.uk/index.php#calendar. These work fine on IE and Chrome, taking the browser to the specified id on the page. However, newer versions of Firefox seem to have an ...

Javascript background image rotation causes a sudden jump to the top of the webpage

I am struggling with a JavaScript issue that I can't seem to figure out. I'm very new to this so any help would be greatly appreciated. I found some code that loads random images into a div element and made some modifications to add a bit of rand ...

Having trouble rendering a dynamic table with JavaScript utilizing a JSON object

I am struggling to retrieve data in JSON format and display it in a table. Despite trying various methods, I have been unable to make it work. Below is the code for this function. Can someone please assist me in identifying what is wrong with it? As of now ...

Solving the challenge of handling multiple text inputs in Laravel Blade using Vue.js v-model within a @

Hello, I am encountering an issue with my Laravel application that displays posts and comments. The problem lies with the Vue v-model in the input text when using the @foreach directive in Blade. Each post is showing the comments input box, but when I ente ...

"Instant Elementor offers seamless CSS transitions for an enhanced user experience

UPDATE: Issue resolved, discovered that Elementor was overriding certain styles. Please refer to my solution below. I'm attempting to add animation to a simple max-height property of a div, but the transition isn't working as expected. It transi ...

A fresh perspective on incorporating setInterval with external scripts in Angular 7

Incorporating the header and footer of my application from external JavaScript files is essential. The next step involves converting it to HTML format and appending it to the head of an HTML file. private executeScript() { const dynamicScripts = [this.app ...

Can CSS be used to automatically focus on a div element?

Can CSS be used to set autofocus on a div element? <div class="form-group" style="margin-left:10px"> <label for="organizationContainer" class="nmc-label">@Res.GroupStrings.CreateNewGroupOrganization</label> <div id="organiza ...

Tips for incorporating API-provided CSS values into AngularJS expressions for stylish card customization

I am in the process of creating unique Pokemon cards that pull data from an API. My question is, how can I apply specific CSS styling to each card directly from the API, similar to how I have utilized AngularJS to render the information? So far, I have su ...

Trigger an action upon checking a checkbox in an Angular2 application

As a newcomer to Angular2 and web development in general, I am looking to implement an action that will modify a parameter value of an object in the Database when a checkbox is checked or unchecked using Material-Design. I attempted to use [(ngModel)], but ...

Styling Iframe Borders with HTML and CSS: A Guide to Custom

Hello there, I am still learning the ropes of HTML and CSS and I have a question regarding styling a div with CSS to look like a default iframe in Internet Explorer. Despite my efforts and searches on Google, I haven't been able to achieve the desired ...

The varying website layouts across different devices

I recently encountered an issue while working on my WordPress website. When I view the page, some features behave differently than expected. One example is a banner that moves in and out based on scroll position (using jQuery Waypoints). Interestingly, whe ...

What is the most efficient way to retrieve key pair values of separate elements in an array with just one API request?

The API I am working with returns an array of elements, each containing multiple key-value pairs. An example of a similar API response can be seen here: , where an array of poems is returned. [ { "title": "...." "content": "..." "url" : "..." } ...

Making sure that res.download() is called only after a specific function has finished executing

In my current project, I am utilizing Express.js and HTML to handle the process of retrieving, processing, and then downloading data to a file upon clicking the submit button in an HTML form. The issue I am facing is that the res.download() function withi ...

Unable to retrieve data from Meteor find query

I have a collection created in collections.js portfolioItems = new Mongo.Collection('portfolioitems'); This collection is then subscribed to in subscriptions.js Meteor.subscribe('portfolioitems'); And published in publications.js M ...

How to handle redirection after login and logout on an Angular Identity Server?

In my Angular project based on .NET Core, I incorporate IdentityServer and customize the Identity Server razor login page. While most of the pages are Angular with lazy-loading features, there are specific scenarios I need to address for post-login and log ...

Filter a Vue table by column name with specific conditions

I am working on code that filters a table based on user input. The process involves selecting a column from a drop-down menu, choosing an operator from another drop-down menu, and entering a search value. I want to filter the table based on these criteria. ...