Thursday, June 6, 2024
 Popular · Latest · Hot · Upcoming
4
rated 0 times [  8] [ 4]  / answers: 1 / hits: 70435  / 11 Years ago, thu, september 19, 2013, 12:00:00

I have a div with id=content-area, when a user clicks outside of this div, I would like to alert them to the fact that they clicked outside of it. How would I use JavaScript to solve this issue?



<div id = outer-container>
<div id = content-area>
Display Conents
</div>
</div>

More From » html

 Answers
3

In pure Javascript



Check out this fiddle and see if that's what you're after!



document.getElementById('outer-container').onclick = function(e) {
if(e.target != document.getElementById('content-area')) {
document.getElementById('content-area').innerHTML = 'You clicked outside.';
} else {
document.getElementById('content-area').innerHTML = 'Display Contents';
}
}


http://jsfiddle.net/DUhP6/2/


[#75595] Wednesday, September 18, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jeremiahianx

Total Points: 629
Total Questions: 106
Total Answers: 112

Location: Djibouti
Member since Sun, Feb 27, 2022
2 Years ago
;