ERROR: The property "width" cannot be read from a null value in custom.js line 6

When attempting to center the image, an error is displayed in the browser stating "cannot read property width of null." I have tried dividing the screen width by 2 but I am unable to understand why this error is occurring.


var mobile = document.getElementById("mobile");
var monitor = document.getElementById("monitor");
var tab = document.getElementById("tab");
var header = document.getElementById("header");
var position = (screen.width - monitor.width)/2;
monitor.style.left = position+"px;";
function initScroll(){
  if(window.pageYOffset >500){
    mobile.style.left = "300px";
    tab.style.right = "250px";
    header.style.height = "60px";
    header.style.fontSize = "25px";
  }else{
    header.style.height = "60px";
    header.style.fontSize = "25px";
    mobile.style.left = "0px";
    tab.style.right = "0px";
  }
}
window.addEventListener("scroll",initScroll);
*{padding: 0;margin: 0;font-family:arial;}
#header{height:100px;background-color: #354458;font-size: 40px;color:#fff; text-align:center;line-height:2.5;
position:fixed; width:100%;z-index:20;
  -moz-transition: 2s height, 2s font-size;
  -o-transition: 2s height, 2s font-size;
   -webkit-transition: 2s height, 2s font-size;
   transition: 2s height, 2s font-size;
}
#banner{background: #3a9ad9;height:400px;position:fixed;width:100%;
top:100px;font-size:50px; text-align: center; color
  :#fff;z-index:10;
}
#banner > * {
  margin-top:30px;
}
#content{
  top:500px;
  position:relative;
  height:1000px;
  padding-top:200px;
  background-color: #fff;
  z-index:15;
}
#mobile{
  position: absolute;
  left: 0;
  z-index: 15;
  top: 470px;
  -moz-transition: 2s left;
  -o-transition: 2s left;
   -webkit-transition: 2s left;
   transition: 2s left;
}
#monitar{position: relative;}
#tab{
  position: absolute;
  right: 0;
  z-index: 15;
  top: 385px; 
  -moz-transition: 2s right;
  -o-transition: 2s right;
   -webkit-transition: 2s right;
   transition: 2s right;

}
<html>
<head>
  <title>Java script Scroller</title>
  <meta  charset="utf-8"/>
  <link  rel="stylesheet" type="text/css" href="./css/style.css"/>
  <script src="./js/custom.js"></script>
</head>
<body>
         <div id="wrapper">
                <div id="header">Header</div>
                <div id="banner">
                  <h1>My Animation</h1>
                  <h2>First collapsable header</h2>
                  <h3>Apurva Kinkar</h3>
                </div>
                <div id="content">
                   <img id="mobile" src="./img/1.jpg" />
                   <img id="monitar" src="./img/2.png" />
                   <img id="tab" src="./img/3.jpg" />
                </div>
         </div>
</body>
</html>

Answer №1

You made a typo error in your <img> tag, it should be monitor instead of monitar

 <img id="monitar" src="./img/2.png" />

To correct this, change it to,

 <img id="monitor" src="./img/2.png" />

I hope this clarifies things for you!

Answer №2

Firstly, there seems to be a mistake in the image id.

Update it like this :-

<img id="monitar" src="./img/2.png" />

to

<img id="monitor" src="./img/2.png" />

Even after making this change, you may encounter the same issue. This is because the img element is not yet added to the DOM when the JavaScript code executes. To fix this, ensure that the JavaScript code runs within the document.ready function (after the img with id "monitor" is present in the DOM).

Check out the functional demo which leverages jQuery's document.ready() function.

You can also refer to this example if you prefer not to use jQuery.

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

Reorganize files with sequential numbers using Gulp, starting from the highest number within the directory

I am looking to create a gulp task that moves files from one folder to another while renaming them with sequential numbers. Here is the current task I have: var index = 0; gulp.task("jpg", function () { return gulp.src('img/new/**.{jpg,JPG}&ap ...

Transferring information from ng-Dialog HTML to a controller

Having some issues with ng-Dialog. When I include the ngDialog controller option, it works. I can retrieve the value of $scope.descriptionText from <p>Description:</p> <textarea ng-model="descriptionText"></textarea> Now, whe ...

Embed the Hero slides directly into the gatsby-image-plugin

Looking to transition my "Home hero slide" from hardcoded to dynamic using the gatsby-image-plugin. Visit my site for reference. Current "hardcoded" sections: HeroSlider.js New code here... HeroSlider.styles.js New code here... Slide.js New code ...

Employ JQuery to insert a hyperlink into an array of items

I am currently working on a project where I am using Python to fetch data from a Postgres database. The data is then displayed in an HTML table by iterating through the content. <tbody id="myTable"> {% for item in content %} &l ...

Locate within an HTML table (inside a specific td child tag) and conceal the row

I am trying to search for content within the H3 tag only, not the TD tag. If a result is found in the table, I want the corresponding TR to be hidden. -H3 is a child tag under TD. For example: <tr> <td> <h3>Example</h3> & ...

Utilizing a NodeJs Express app.get to manage both query strings and parameters

In my quest to develop a REST Service, I have crafted a route that triggers the execution of a stored procedure and retrieves JSON results. app.get('/spparam', function (req, res) { var sql = require("mssql"); // Database configuration var id=0 ...

**Preventing Duplicate Submissions with Form**

Hi everyone, I am in the process of developing a web application using jquery/mvc/knockout that involves sending data to the server through an expensive transaction using $.post. To prevent users from double clicking or getting impatient and clicking mul ...

Using select2 scss from the node_modules folder

Looking for help with importing select2 scss from node_modules. I successfully installed the select2 by running the command: npm install select2 which created a select2 folder in the node_modules directory. Can anyone guide me on how to import the css f ...

Running several setInterval functions on a Node server

My application is a gaming platform that challenges users to complete tasks within a 30-minute timeframe using a node backend. Whenever a user initiates a game, a unique setInterval function activates on the server side. This timer counts down for 30 minu ...

What is the best approach to designing a dynamic class toggler for a React sidebar?

I am trying to add a specific class to a clicked link within a sidebar. While this works for one link, when I attach the handler to another link and click it, both links get assigned with the new class. Is there a way to make this functionality more like ...

Add a distinctive tooltip to the final element in a table row within a loop using JQuery

My challenge involves appending unique tooltips to the last item in a row of data, where each tooltip is specific to that particular row. However, when I add a new tooltip for the latest item, it ends up replacing the tooltips from previous rows. The desi ...

Ways to achieve outcomes from functions employing concatMap within rxjs?

When calling two functions, I make use of fn1 and fn2. To execute them one after the other, I utilize concatMap. I choose not to use exhaustMap and switchMap as they can result in nested "callback-hell". exhaustMap(() => fn1().pipe( swit ...

Retrieve values from an array containing objects using JavaScript

As I am exploring Firebug, I encountered an issue where the console log of an array shows: Array [ ] Even though in the right panel of Firebug it is visible that the array contains 3 objects, I am unable to access them. Is there a way to retrieve these va ...

Prevent legend strike-through on click in Vue Chart.js

Recently, I made the transition from vue 2 to vue 3 on my website and part of that process involved updating vue-chartjs and chartjs too. However, after modifying the legend text of my pie chart using the generateLabels option (as seen below), the striket ...

The absence of CSV may be attributed to a reference error

I'm new to all of this, so I believe it's a simple mistake on my end, but I can't seem to get it to work. After deploying the code below and clicking on the button, nothing happens. When I inspect the html in my browser, I see an error mess ...

Adding Node Modules during the setup of an ElectronJS application

Hey there! I'm currently working on an ElectronJS application designed for developers. One of the key features is checking for the presence of NodeJS on the user's computer. If it's not detected, the app will automatically download and insta ...

The outcome of comparing with Bcrypt is consistently a negative result

bcrypt.compare() is consistently returning false when used in conjunction with this code within the user model. It appears that this issue is specific to bcrypt-nodejs. User.pre('save', function (callback) { this.password = bcrypt.hashSync(thi ...

Encountered an error with the Angular 1 component router: Unable to access property 'startsWith' as it is undefined

I've been utilizing the Angular 1 component router and encountering some strange issues. Everything works perfectly when I run the website locally, but as soon as I try to access it from another PC, I'm hit with a JavaScript error. It keeps th ...

Why is text overlapping padding in Bootstrap 4? What can be done to prevent this issue?

I've set up a basic bs4 layout where the left-menu is represented by <div class="col-md-2">, the center-main-content by <div class="col-md-6">, and the right-menu by <div class="col-md-4">. Using Chrome ...

Troubleshooting Texture Compatibility Issue with ThreeJS ShaderMaterial on iOS Devices

Seeking assistance with shaders in Threejs. I have a plane that requires a mixture of 10 different textures; currently using ShaderMaterial and passing all textures for blending. Below is my Fragment Shader code: vec3 CFull = texture2D(tFull, vUv).rgb; vec ...