I’m creating a React project which includes images.
In my project I want to get the width and height of image that uploaded.
For that I am using
const image = document.getElementById("inputimage");
const width = Number (image.width);
const height = Number (image.height);
console.log( width, height );
Image tag in another page
<img id='inputimage' alt='image' src={imageUrl} width="500px" height="auto" />
Here Number() is used to convert the image.width and image.height into numbers for future calculations.
Whenever I run this code I didn’t get the output.
const image = document.getElementById("inputimage");
const width = image.width;
const height = image.height;
console.log( width, height );
But when I remove the Number() then I got output.
Does anyone know why I didn’t get output when Number() is used ?
There is anything I can do for converting the image.width and image.height into numbers?
>Solution :
I think you can use parseInt(image.width) (or image.height).