I have a textarea, and a submit button. When a user clicks outside of the textarea a script is run, and if there is nothing in the textarea, it hides the submit button. However, I don’t want it to be if you click outside of the textarea because when you click the submit button it runs the action, and hides the button. which it shouldn’t. So I need to detect when the user clicks outside of the wrapper div of the textarea and submit button and run the script.
<div class="wrapper">
<textarea class="textarea"></textarea>
<input type="submit" class="submit" value="submit"/>
</div>
Check the ‘target’ property of the event, which will tell you which element was actually clicked on. You can then do a conditional statement to ensure that the submit button didn’t dispatch the event.
jQuery docs: http://api.jquery.com/event.target/
Mozilla docs: https://developer.mozilla.org/en-US/docs/DOM/event.target