Tim Marshall Tim Marshall - 5 months ago 11
jQuery Question

On List Click > Get Root UL ID

My list looks like this;

<ul id="SiteNav" class="Mail New">
<li class="Send"><a href="javascript:;" data-action="mail-send">Send</a></li>
<li class="Insert">
<a href="javascript:;" class="Drop">Insert <i class="fa fa-angle-down"></i></a>
<ul>
<li><a href="javascript:;" data-action="mail-files-as-attachments">Files as attachments</a></li>
<li><a href="javascript:;" data-action="mail-pictures-inline">Pictures inline</a></li>
<li><a href="javascript:;" data-action="mail-share-from-BananzaCloud">Share from BananzaCloud</a></li>
</ul>
</li>


And then my script starts with;

$(document).on('mousedown', function(e) {


On all links clicked on this menu, I want to check if the $(e.target) is a child of
id="SiteNav"

Answer

You can listen all document mouse down events, and filter those based on event target.

$(document).on('mousedown', function(e) {

        if ($(e.target).parents('#SiteNav').length){

          //your code
        }
    })

or a better approach to listen directly on the menu container

$('#SiteNav a').on('mousedown', function(e) {
console.log('test');
})
Comments