Jordan
Jordan

Reputation: 105

If element is clicked do this funciton unless this child is clicked

In SharePoint, I am trying to be able to click anywhere in the group by header element and have the group expand/collapse. I have gotten it to work partially. Right now I can click anywhere in the element except on the <a> tag that opens it. How do I set the conditional to run the function when anywhere is clicked except the <a> tag?

Here is what I have:

$(document).ready(function() { 
var groupby = $(".ms-gb");
groupby.click(function(){
    var groupbyAnchor = $("a", this);
    groupbyAnchor = groupbyAnchor[0];
    expandThisGroup(groupbyAnchor)}); 
});

function expandThisGroup(anchor) {
anchor.click(); 
}

Upvotes: 0

Views: 61

Answers (2)

Davion
Davion

Reputation: 911

You might do it like this. Pass the click event target to the function, if the target is not the given anchor, do something, else do nothing.

$(document).ready(function(){

    var groupby = $(".ms-gb");

    groupby.on('click', function(e){

        var $target = e.target,
            $anchor = $('a', this);

        if(!($target == $anchor)){
            // do function
        }else{
            return false;
        }
    });
});

Upvotes: 1

Barmar
Barmar

Reputation: 781059

Put another handler on the child element that prevents event propagation:

groupby.find("a").click(function(e) {
    e.stopPropagation();
});

Upvotes: 1

Related Questions