Table row click, except element

Click handler to a Table row element except for a sub-element

by Gautham PJ

HTML

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<table class="table">
    <thead>
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Details</th>
            <th>Status</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>123</td>
            <td>Name 1
                <div class="pull-right sub-menu">
                    <div class="btn-group">
                        <button type="button" class="btn btn-default btn-xs dropdown-toggle edit-asset" data-toggle="dropdown" style="border: none;">
<i class="fa fa-chevron-down"></i>

                        </button>
                        <ul class="dropdown-menu pull-right" role="menu">
                            <li><a href="#">Edit</a></li>
                            <li><a href="#">Delete</a></li>
                            <li><a href="#">Block</a></li>
                        </ul>
                    </div>
                </div>
            </td>
            <td>New User</td>
            <td>Pending</td>
        </tr>
    </tbody>
</table>

JavaScript

$(function() {

    $('table tr').on('click', function(e) {
        
        // The below "if" condition is the ANSWER.
        if (!$(e.target).closest('.sub-menu').length) {
            alert('Clicked');
        }
        
    });
});