Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-21-2010, 10:12 PM   PM User | #1
johnmerlino
Regular Coder

 
Join Date: Oct 2009
Posts: 189
Thanks: 38
Thanked 3 Times in 3 Posts
johnmerlino is an unknown quantity at this point
Event Order Issue

Hey all,

I have an object called Table that gets instantiated:
Code:
var tableReady = new Table($(this))
This Table in turn instantiates a header:
Code:
if(typeof header.attr('data-sorter') !== 'undefined') headers.push(new Header(header));
A method is attached to the Header object called attach() which simply passes in a string 'click':
Code:
	var Header = function(header){
		
		this.header = header;
		this.addEventHandler(this.header, "click", attach("click"), true); 
	}
The addEventHandler() function is declared below:
Code:
function addEventHandler(oNode, evt, oFunc, bCaptures)
{
	if (typeof(window.event) != "undefined")
		oNode.attachEvent("on"+evt, oFunc);
	else
		oNode.addEventListener(evt, oFunc, bCaptures);
}
While the addEventHandler() function is called during Header instantiation, which in turn calls attach(), attach() is never executed unless there is a click event called on header object:
Code:
	Header.prototype = {
		attach: function(event){
			var obj = this;
			if(event == "click"){
				obj.header.bind("click", function(e){ 
					obj.children()[0].preventDefault();
					return obj.calculate();
				})
			}
		},
I call preventDefault() (I'm using the jquery library as well) to try to prevent the link from responding to a click by targeting obj.children()[0] which refgers to the nested link.

Unfortunately this doesn;t work. The link is still triggered anyway. How can I force the browser not to respond to the link in the above situation, preferably using an OOP solution?

Thanks for response.
johnmerlino is offline   Reply With Quote
Old 10-22-2010, 12:39 AM   PM User | #2
mrhoo
Regular Coder

 
Join Date: Mar 2006
Posts: 708
Thanks: 30
Thanked 127 Times in 118 Posts
mrhoo will become famous soon enoughmrhoo will become famous soon enough
You don't preventDefault on the element, but on the event.
It is the event that bubbles, the element isn't going anywhere.

You may want to use e.preventDefault, but you definitely need to call
e.stopPropagation() after you handle it, to keep that event from percolating upwards.

Or for IE before #9, set window.event.cancelBubble to true and (maybe) window.event.returnValue to false.
mrhoo is offline   Reply With Quote
Old 10-22-2010, 01:36 AM   PM User | #3
johnmerlino
Regular Coder

 
Join Date: Oct 2009
Posts: 189
Thanks: 38
Thanked 3 Times in 3 Posts
johnmerlino is an unknown quantity at this point
I changed it to this:
Code:
	var Header = function(header){
		
		this.header = header;
		this.attach("click");
	}
	
	Header.prototype = {
		attach: function(event){
			var obj = this;
			if(event == "click"){
				obj.header.bind("click", function(e){ 
					e.stopPropogation();
					return obj.calculate();
				})
			}
		}
Still the link responds to click. Any ideas?
johnmerlino is offline   Reply With Quote
Old 10-22-2010, 01:38 AM   PM User | #4
johnmerlino
Regular Coder

 
Join Date: Oct 2009
Posts: 189
Thanks: 38
Thanked 3 Times in 3 Posts
johnmerlino is an unknown quantity at this point
By the way, the link is nested in th tag:
Code:
	<th data-sorter="sort sort-asc" data-sort-expression="<%= :school_num %>"><a href="">School</a></th>
johnmerlino is offline   Reply With Quote
Old 10-22-2010, 01:48 AM   PM User | #5
johnmerlino
Regular Coder

 
Join Date: Oct 2009
Posts: 189
Thanks: 38
Thanked 3 Times in 3 Posts
johnmerlino is an unknown quantity at this point
Even if I target the child element and return false, the link still is triggered:
Code:
		attach: function(event){
			var obj = this;
			if(event == "click"){
				obj.header.children[0].bind("click", function(e){ 
					return false;
				})
			}
		}
johnmerlino is offline   Reply With Quote
Old 10-27-2010, 09:33 PM   PM User | #6
johnmerlino
Regular Coder

 
Join Date: Oct 2009
Posts: 189
Thanks: 38
Thanked 3 Times in 3 Posts
johnmerlino is an unknown quantity at this point
I look back at this thread and it's quite clear what the problem was. It was an issue of cross-browser inconsistency. This worked:
Code:
		attach: function(event){
			var obj = this;
			if(event == "click"){
				obj.header.bind("click", function(e){ 
					stopEvent(e);
					return obj.calculate();
				})
			}
		}

	function stopEvent(e){
		(window.event != undefined) ? window.event.cancelBubble = true : e.stopPropagation();
	}
I presume that the jquery library has something that accomodates this. But i mainly use the library for DOM traversing, as I'm not a big fan of the for loop.
johnmerlino is offline   Reply With Quote
Reply

Bookmarks

Tags
click, event, link, object-oriented

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:12 AM.


Advertisement
Log in to turn off these ads.