using
//If clicked element id is not contents if(event.target.id !== 'contents') { alert(".."); }
EDIT - if the structure is used to use your comment:
if($(event.target).closest('div#contents').length === 0){ alert('there is no div with id === "contents" in the ancestors of the clicked li'); }
EDIT 2 is an explanation of my code. If you have this markup
<div id="contents"><ul><li>Item1</li><li>Item2</li></ul></div>
this code means
Nicola Peluchetti
source share