2

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
    }
}
1
  • The same way you added the resize event but with click instead of resize Commented Apr 12, 2018 at 16:49

2 Answers 2

3
var element = document.getElementById("id");
element.addEventListener("click", function(){
});

fiddle

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.