0

I am trying to prevent a click form bubbling to the parent <a> HTML tag. Here is my code:

var $result = $("<a href='" + result["link"] + "' class='list-group-item'></a>");
var $title = $("<h4 class='list-group-item-heading'>" + result['title'] + "</h4>");
var $snippet = $("<p class='list-group-item-text'>" + result['htmlSnippet'] + "</p>");

// some code omitted for brevity

var $expandBooksbutton = $("<button type='button' id='btn-showMoreBooks' class='btn btn-default btn-xs'><span class='glyphicon glyphicon-resize-full'></span> expand</div></button>");

$expandBooksbutton.on('click', function(event){
    event.stopPropagation();
    var height = $('#book-container').height();
    if(height != 200){
        $('#book-container').height(200);
    }
    else{
        $('#book-container').height(100);
    }
});

I am wrapping the whole thing in the anchor tag <a> because I am crafting a search result item.

Regardless of adding event.stopPropogation(); the click event on the <a> still fires.

Vinay Joseph
  • 5,515
  • 10
  • 54
  • 94

3 Answers3

1

You can try this

$(<a tag selector>).on('click',function(e){
    e.preventDefault();
});
Tejasva Dhyani
  • 1,342
  • 1
  • 10
  • 19
0

There is a difference between preventing bubbling and cancelling out the default browser behaviour. Try adding

return false;

in the end of your handler function, like this

    $expandBooksbutton.on('click', function(event){
    var height = $('#book-container').height();
    if(height != 200){
        $('#book-container').height(200);
    }
    else{
        $('#book-container').height(100);
    }
    return false;

});

This should be the solution if I understood the question correctly.

UPDATE

Sorry, I should have mentioned that there's also an alternative for this (which actually I should have given as an advice primarily) and this is:

event.preventDefault();

This function actually prevents the default behaviour, whereas

return false;

does both (stops bubbling & prevents default behaviour)

There's more info on this here: event.preventDefault() vs. return false

Community
  • 1
  • 1
Oleg Berman
  • 664
  • 5
  • 18
0

Without knowing your full DOM structure, this will be slightly abstract. However event handlers bound to an element before it is added to the DOM aren't actually triggered.

From the jQuery docs:

Event handlers are bound only to the currently selected elements; they must exist on the page at the time your code makes the call to .on().
To ensure the elements are present and can be selected, perform event binding inside a document ready handler for elements that are in the HTML markup on the page. If new HTML is being injected into the page...attach event handlers after the new HTML is placed into the page. Or, use delegated events....

So basically you have two options: Create a handler for a delegated event and bind that to the parent element after the parent is added to the DOM. Note that this approach requires that the parent be nested inside the <a> as well.

$(parent-elem).on('click', '#btn-showMoreBooks', function(e) {
    // your handler logic
});

Of course, you can always keep your logic as-is, but bind the handler to the element only after it's added to the DOM. This is reasonable as long as you won't be re-creating that element dynamically more than once after the initial page load.

nbrooks
  • 18,126
  • 5
  • 54
  • 66