Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery click event for document but ignore a div

I have a online Slide Show I'm working on using jQuery. I have use $(document).click event to detect when a user clicks on the page to know when to show the next bullet point in the slide or to move to the next page.

The problem I'm running into is my job had me insert a comment box on the bottom of the page and when ever someone clicks on the comment box or the save comment button it also fires the click event for the page.

Is there a way I can have the click event for the entire page but ignore it when someone clicks in the DIV the Comment box/Save button are in?

like image 279
RoboDev Avatar asked Feb 10 '09 17:02

RoboDev


People also ask

How do you exclude an element in click?

To remove an element from the DOM on click: Select the DOM element. Add a click event listener to the element. Call the remove() method on the element in the event handler.

What is the difference between .click and .on (' click in jQuery?

So onclick creates an attribute within the binded HTML tag, using a string which is linked to a function. Whereas . click binds the function itself to the property element.

When to use event preventDefault()?

The preventDefault() method cancels the event if it is cancelable, meaning that the default action that belongs to the event will not occur. For example, this can be useful when: Clicking on a "Submit" button, prevent it from submitting a form. Clicking on a link, prevent the link from following the URL.

Why we use e preventDefault()?

The preventDefault() method is used to prevent the browser from executing the default action of the selected element. It can prevent the user from processing the request by clicking the link.


1 Answers

You will most likely need to stop the propagation of events in your Comments div using the event object's stopPropagation() method:

$('#comments').click(function(e) {
  e.stopPropagation();
});

if that doesn't work try using preventDefault():

e.preventDefault();
like image 172
bendewey Avatar answered Sep 20 '22 04:09

bendewey