My aim is to add an onClick event to an element when the screen is below 700px wide and to remove the clickable area if its above 700px. Am I going about this in a good way? How would I add onClick="myFunction()" to a div in HTML?
myFunction();
window.addEventListener("resize", function(event) {
myFunction();
});
function myFunction() {
var i = document.all?document.body.clientWidth:window.innerWidth;
if(i > 701) {
// Adds onClick
}
if(i < 701) {
// Removes onClick
}
}
var element = document.getElementById("id");
element.addEventListener("click", function(){
});
As already said by Timovski to assign onClick to div , give some id to the div and declare var.
var element = document.getElementById('divID');
than simply add eventListener to the declared var.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.