Bug Tracker

Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#8521 closed bug (worksforme)

There is funny bahaviour with mutiple bindings 'live' and 'return false'.

Reported by: MICKtheBIG Owned by: MICKtheBIG
Priority: undecided Milestone: 1.next
Component: event Version: 1.5.1
Keywords: Cc:
Blocked by: Blocking:

Description

I've expierienced this issue with more than one event handler on links in Firefox 3.6.3.

Try the following code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de" lang="de" >
    <head>
        <title>Live-Binding-Test</title>
		
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />

        
        <script type="text/javascript" src="js/jquery.js"></script>

        <script type="text/javascript">
        	$(document).ready(function() {
        		
        	    $(".j_controlButton").click(function(){
        		    return false;
        		});
        	    
        		$(".j_controlButton").live("click", function(){alert("click 'live' binding callback executed")});
        		$(".j_controlButton").bind("click", function(){alert("click 'bind' binding callback executed")});
        		
        		
        		$("body").append('<a href="#" class="j_controlButton">Dynamic DOM-Link</a>');
        		
            });
        </script>
    </head>
    <body>
        <a href="#" class="j_controlButton">Static DOM-Link</a> <br /> <br />
    </body>
</html>

With "return false;" at line 16 only the event with "bind" is triggered for the "Static DOM-Link".

Then comment out line 16 (return false;) After this you'll see, that the live binding is also triggered for the "Static DOM-Link". This is from my point of view a funny behaviour. Eigther the "bind"-Binding shouldn't work all the time for the "Static DOM-Link" or the live-binding should work with or without "return false;". From my point of view the live-binding should work with and without "return false;" from line 16.

I hope you understand this issue and it's not a duplicated issue....

Change History (6)

comment:1 Changed 8 years ago by Rick Waldron

Component: unfiledevent
Owner: set to MICKtheBIG
Status: newpending

Thanks for taking the time to contribute to the jQuery project! Please provide a reduced jsFiddle test case to help us assess your ticket!

Additionally, test against the jQuery 0 GIT version to ensure the issue still exists.

comment:2 Changed 8 years ago by MICKtheBIG

Status: pendingnew

Live-Binding should work on "Static DOM-Link" but does not: http://jsfiddle.net/5JtSb/

Live-Binding now works on "Static DOM-Link" with removing return false: http://jsfiddle.net/Q7uyt/1/

The thing is, that "bind" always works on the "Static DOM-Link" even with return false. The live binding only works when return false ist removed

comment:3 Changed 8 years ago by dmethvin

Resolution: worksforme
Status: newclosed

That is correct behavior. When you return false from an event handler directly bound to the element, it prevents the event from bubbling to the document where the .live() would see it.

comment:4 Changed 8 years ago by ajpiano

MICKtheBIG replied:

"Yes, I know that it does this ("it prevents the event from bubbling to the document") but why is it then working with "bind"? Then you actually said that "bind" isn't working correctly. But it would have horrible side effects if you would change that. I still think that "bind" and "live" is behaving inconsistency in this case."

but i accidentally deleted it because the comment was showing up twice. So here it is again.

comment:5 Changed 8 years ago by dmethvin

There are TWO event handlers directly bound to the element. A return false does a event.preventDefault() and an event.stopPropagation() to stop the event from bubbling *up* the DOM tree. However, it does not event.stopImmediatePropagation() which would prevent further event handlers *at the same level* from executing.

comment:6 Changed 8 years ago by MICKtheBIG

Alright, I was just wondering. I can deal with this. No problem. I just thought I might inform you about this behaviour because I thought it wasn't supposed to behave like it does at the moment. So everything is nice. I just couldn't find a hint for this in the API docs. I should have inspected this issue in the jQuery-Core but I didn't....

Note: See TracTickets for help on using tickets.