Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
94
rated 0 times [  100] [ 6]  / answers: 1 / hits: 16130  / 13 Years ago, sun, september 18, 2011, 12:00:00

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.


More From » css

 Answers
17

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:



<div id=outer>
<div id=inner></div>
</div>


and the JS code:



$('#outer').click(function() {
alert('click');
});

$('#inner').click(function(e) {
e.stopPropagation();
});


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.


[#90037] Friday, September 16, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jeanettee

Total Points: 209
Total Questions: 97
Total Answers: 98

Location: Papua New Guinea
Member since Thu, Jul 9, 2020
4 Years ago
;