I have an HTML element with some padding. I would like to detect for clicks on that element’s padding. That is, I don’t want the event to fire when the user clicks on the content, just the padding.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Create an inner element which has 100% height/width so it fills out the whole element. Then register a click event handler on this event and prevent bubbling of the event.
Here’s an example (using jQuery): http://jsfiddle.net/ThiefMaster/QPxAp/
The markup:
and the JS code:
Since events bubble, the click event on the inner element hits this element first – stopping its propagation will prevent it from ever reaching the outer element, so its click event handler only triggers if the are that belongs to the element but is not covered by the inner element is clicked.