'"event" is deprecated, what should be used instead?
I'm using a found code where "event" is used. It works, but I would like to know what should be used instead.
I'm a novice programmer and there are a concepts that I'm missing. in this case, I'm using a code I found in the web, that can be found in the next link: https://codepen.io/galulex/pen/eNZRVq
PhpStorm shows me that "event" on onmousemove="zoom(event)" is deprecated. I have tried erasing it but it does not work that way. I would like to know what should I use instead of event.
<figure class="zoom" onmousemove="zoom(event)" style="background-image: url(//res.cloudinary.com/active-bridge/image/upload/slide1.jpg)">
<img src="//res.cloudinary.com/active-bridge/image/upload/slide1.jpg" />
</figure>
function zoom(e){
var zoomer = e.currentTarget;
e.offsetX ? offsetX = e.offsetX : offsetX = e.touches[0].pageX
e.offsetY ? offsetY = e.offsetY : offsetX = e.touches[0].pageX
x = offsetX/zoomer.offsetWidth*100
y = offsetY/zoomer.offsetHeight*100
zoomer.style.backgroundPosition = x + '% ' + y + '%';
}
Solution 1:[1]
I was getting this error on VS code while using it like this
document.addEventListener("keydown", function()
{
console.log(event);
});
The warning got solved using the below code
document.addEventListener("keydown", function(event)
{
console.log(event);
});
Reason- It's missing the event parameter in the event handler function. It ends up using the global window.event which is fragile and is deprecated.
Solution 2:[2]
I had this same problem. I found this code worked for me like event used to.
function hide(e){
e.currentTarget.style.visibility = 'hidden';
console.log(e.currentTarget);
// When this function is used as an event handler: this === e.currentTarget
}
var ps = document.getElementsByTagName('p');
for(var i = 0; i < ps.length; i++){
// Console: print the clicked <p> element
ps[i].addEventListener('click', hide, false);
}
// Console: print <body>
document.body.addEventListener('click', hide, false);
// Click around and make paragraphs disappear
Solution 3:[3]
You can do this too (in react),
<input type="file" onChange={event => handleFileChange(event)} />
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ayan |
| Solution 2 | Christopher |
| Solution 3 | Akshay K Nair |
