I have a textarea within a div.
I want to be able to click on the overlay.
But what happens is the textarea catches the click as well as the div.
<div id="d" name="d"><textarea id="ta" name="ta">my text</textarea></div>
<script type="text/javascript">
$('#d').click(function(){alert("clicked on div");)};
$('#ta').click(function(){alert("clicked on textarea");)};
</script>
I just want the div alert to fire, and not the textarea.
First permuting the order of the brackets at the end to prevent javascript errors:
Then I get both alerts, see this example.
=== UPDATE ===
The event bubbles from the upper dom element down to the lowest (if you don’t prevent bubbling). So you can stop bubbling the event after the
textarea, but you can’t prevent to get it in thedivand not in thetextarea. You have to change the order of the html elements (divandtextarea) and then add areturn false;at the end of thedivclick handler or add anevent.stopPropagation();. Also see my updated example or my next example.