0

Right now this will work when I click "box1" but not "box2". I'd like to have a single tapBoxes variable that listens for a click on either box1 OR box2, and triggers the function. Any ideas?

var tapBoxes = document.getElementById("box1") || document.getElementById("box2"); 

tapBoxes.onclick = function() {
      ...
    }

2 Answers 2

1

Define the function first. Then assign it to all the buttons you want.

tapBoxesClick = function() {
 ...
 }

document.getElementById("box1").addEventListener("click", tapBoxesClick, false);
document.getElementById("box2").addEventListener("click", tapBoxesClick, false); 
Sign up to request clarification or add additional context in comments.

Comments

0

Give your elements a class, lets say myBox, then use jquery to do what you want:

$(document).ready(function(){
   $('.myBox').click(function(){
     //do what you want with the clicked box

   })
})

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.