<?xml version="1.0" encoding="ISO-8859-1"?><!-- generator="wordpress/1.5.1.3" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: Adding events to elements</title>
	<link>http://www.ismaelj.com/articulos/addevent-recoding-contest/</link>
	<description>Artículos sobre JavaScript / CSS / (X)HTML</description>
	<pubDate>Wed, 23 Jul 2008 19:55:51 +0000</pubDate>
	<generator>http://wordpress.org/?v=1.5.1.3</generator>

	<item>
		<title>by: Ismael Jurado</title>
		<link>http://www.ismaelj.com/articulos/addevent-recoding-contest/#comment-3360</link>
		<pubDate>Mon, 07 Jul 2008 17:00:58 +0000</pubDate>
		<guid>http://www.ismaelj.com/articulos/addevent-recoding-contest/#comment-3360</guid>
					<description>Rob, I don't have enough information to guess the right answer. 
buttonID is assigned in HTML to the SPAN or the the IMG? 
And the IMG is surrounded by the SPAN?</description>
		<content:encoded><![CDATA[	<p>Rob, I don&#8217;t have enough information to guess the right answer.<br />
buttonID is assigned in HTML to the SPAN or the the IMG?<br />
And the IMG is surrounded by the SPAN?
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Rob Reid</title>
		<link>http://www.ismaelj.com/articulos/addevent-recoding-contest/#comment-3359</link>
		<pubDate>Mon, 07 Jul 2008 13:36:35 +0000</pubDate>
		<guid>http://www.ismaelj.com/articulos/addevent-recoding-contest/#comment-3359</guid>
					<description>I am having problems with your code in Internet Explorer (6-7) which seems to be related to the this keyword.

I used to use the following functions which do not give me the same problems that I am currently having with your functions. I swapped over to use your functions after reading about the problems they fix that the others didn't and they have worked 
elsewhere perfectly until now with this particular issue.

//old functions that work
function addEvent( obj, type, fn ){ 
	if ( obj.attachEvent ) { 
		obj['e'+type+fn] = fn; 
		obj[type+fn] = function(){obj['e'+type+fn]( window.event );} 
		obj.attachEvent( 'on'+type, obj[type+fn] ); 
	}else 
		obj.addEventListener( type, fn, false ); 
}

function removeEvent( obj, type, fn ) {   
	if ( obj.detachEvent ) { 
		obj.detachEvent( 'on'+type, obj[type+fn] ); 
		obj[type+fn] = null; 
	}else 
		obj.removeEventListener( type, fn, false ); 
} 


The problem is with a WYSIWYG HTML editor that is instantiated on an onclick event and written out to the DOM.
I am looping through all my toolbar buttons adding onclick event handlers to each button in turn.
To take one button in particular the HTML format is this (from IE's generated source)


	
		
	


I am looping through an array of all my buttons within an editor method and adding the event handler like so

Editor.prototype.Create = function()
{
	//some other code
	var self = this;
        //loop through all buttons in turn
	elem = document.getElementById(buttonID);
	addEvent( elem, 'click', function(){ShowDebug('Button 2 OnClick');self.FormatText(this.id,n);} ) 				
	
}

// I have also tried this for buttons I know the ID for.

elem = document.getElementById('ViewText');
addEvent( elem, 'click',  function(){ShowDebug('Button 2 OnClick');self.FormatText('ViewText',n);} );

The event handler seems to be added correctly (from firebuglite debug in IE - I have put some debug into your addEvent function handler when
using attachEvent)


function addEvent(obj,evType,func){
	var r = false;
	if(obj.addEventListener){
		obj.addEventListener(evType, func, false);
		r = true;
	}else if (obj.attachEvent) {
		var id = obj.sourceIndex &amp;#166;&amp;#166; -1;
		ShowDebug(&quot;addEvent to &quot; + id);
		if (!func[evType + id]) {
			var f = func[evType + id] = function(e) {
				var o = document.all[id] &amp;#166;&amp;#166; document;
				o._f = func;
				var s = o._f(e);
				o._f = null;
				return s;
				};
			ShowDebug(&quot;obj = &quot; + obj + &quot; typeof = &quot; + typeof(obj) + &quot; obj.id = &quot; + obj.id + &quot; tagName = &quot; + obj.tagName);
			r = obj.attachEvent(&quot;on&quot; + evType, f);
			obj = null;
		}
	}else{ //if there is already a function for this handler call beforehand
		var ev=&quot;on&quot;+evType;
		var oldevent = obj[ev];
		if (typeof oldevent != &quot;function&quot;){			
			obj[ev]=func;
		}else{			
			obj[ev] = function(){ oldevent();func();}
		}
	}
	return r;
};


1: addEvent to 628
2: obj = [object] typeof = object obj.id = ViewText tagName = IMG

The problem comes whenever I try to click on any of the toolbar buttons. Instead of passing through the reference to the actual IMG tag which it should be doing its passing through the reference for the node above which is the SPAN tag.

// INCORRECT!!!
25: Button 2 Onclick
26: IN FormatText HTMLModeCMStextarea ; CMStextarea ;

Whereas it should be passing though (as Firefox correctly does) the ID for the IMG tag.

// CORRECT!!!
11: Button 2 OnClick
12: IN FormatText ViewSource ; CMStextarea ;

If I revert back to the older functions it works fine. Can you give me some guidance on whats gone wrong and if there is a fix about?

Thanks</description>
		<content:encoded><![CDATA[	<p>I am having problems with your code in Internet Explorer (6-7) which seems to be related to the this keyword.</p>
	<p>I used to use the following functions which do not give me the same problems that I am currently having with your functions. I swapped over to use your functions after reading about the problems they fix that the others didn&#8217;t and they have worked<br />
elsewhere perfectly until now with this particular issue.</p>
	<p>//old functions that work<br />
function addEvent( obj, type, fn ){<br />
	if ( obj.attachEvent ) {<br />
		obj[&#8217;e'+type+fn] = fn;<br />
		obj[type+fn] = function(){obj[&#8217;e'+type+fn]( window.event );}<br />
		obj.attachEvent( &#8216;on&#8217;+type, obj[type+fn] );<br />
	}else<br />
		obj.addEventListener( type, fn, false );<br />
}</p>
	<p>function removeEvent( obj, type, fn ) {<br />
	if ( obj.detachEvent ) {<br />
		obj.detachEvent( &#8216;on&#8217;+type, obj[type+fn] );<br />
		obj[type+fn] = null;<br />
	}else<br />
		obj.removeEventListener( type, fn, false );<br />
} </p>
	<p>The problem is with a WYSIWYG HTML editor that is instantiated on an onclick event and written out to the DOM.<br />
I am looping through all my toolbar buttons adding onclick event handlers to each button in turn.<br />
To take one button in particular the HTML format is this (from IE&#8217;s generated source)</p>
	<p>I am looping through an array of all my buttons within an editor method and adding the event handler like so</p>
	<p>Editor.prototype.Create = function()<br />
{<br />
	//some other code<br />
	var self = this;<br />
        //loop through all buttons in turn<br />
	elem = document.getElementById(buttonID);<br />
	addEvent( elem, &#8216;click&#8217;, function(){ShowDebug(&#8217;Button 2 OnClick&#8217;);self.FormatText(this.id,n);} ) 				</p>
	<p>}</p>
	<p>// I have also tried this for buttons I know the ID for.</p>
	<p>elem = document.getElementById(&#8217;ViewText&#8217;);<br />
addEvent( elem, &#8216;click&#8217;,  function(){ShowDebug(&#8217;Button 2 OnClick&#8217;);self.FormatText(&#8217;ViewText&#8217;,n);} );</p>
	<p>The event handler seems to be added correctly (from firebuglite debug in IE - I have put some debug into your addEvent function handler when<br />
using attachEvent)</p>
	<p>function addEvent(obj,evType,func){<br />
	var r = false;<br />
	if(obj.addEventListener){<br />
		obj.addEventListener(evType, func, false);<br />
		r = true;<br />
	}else if (obj.attachEvent) {<br />
		var id = obj.sourceIndex || -1;<br />
		ShowDebug(&#8221;addEvent to &#8221; + id);<br />
		if (!func[evType + id]) {<br />
			var f = func[evType + id] = function(e) {<br />
				var o = document.all[id] || document;<br />
				o._f = func;<br />
				var s = o._f(e);<br />
				o._f = null;<br />
				return s;<br />
				};<br />
			ShowDebug(&#8221;obj = &#8221; + obj + &#8221; typeof = &#8221; + typeof(obj) + &#8221; obj.id = &#8221; + obj.id + &#8221; tagName = &#8221; + obj.tagName);<br />
			r = obj.attachEvent(&#8221;on&#8221; + evType, f);<br />
			obj = null;<br />
		}<br />
	}else{ //if there is already a function for this handler call beforehand<br />
		var ev=&#8221;on&#8221;+evType;<br />
		var oldevent = obj[ev];<br />
		if (typeof oldevent != &#8220;function&#8221;){<br />
			obj[ev]=func;<br />
		}else{<br />
			obj[ev] = function(){ oldevent();func();}<br />
		}<br />
	}<br />
	return r;<br />
};</p>
	<p>1: addEvent to 628<br />
2: obj = [object] typeof = object obj.id = ViewText tagName = IMG</p>
	<p>The problem comes whenever I try to click on any of the toolbar buttons. Instead of passing through the reference to the actual IMG tag which it should be doing its passing through the reference for the node above which is the SPAN tag.</p>
	<p>// INCORRECT!!!<br />
25: Button 2 Onclick<br />
26: IN FormatText HTMLModeCMStextarea ; CMStextarea ;</p>
	<p>Whereas it should be passing though (as Firefox correctly does) the ID for the IMG tag.</p>
	<p>// CORRECT!!!<br />
11: Button 2 OnClick<br />
12: IN FormatText ViewSource ; CMStextarea ;</p>
	<p>If I revert back to the older functions it works fine. Can you give me some guidance on whats gone wrong and if there is a fix about?</p>
	<p>Thanks
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Ismael Jurado</title>
		<link>http://www.ismaelj.com/articulos/addevent-recoding-contest/#comment-390</link>
		<pubDate>Wed, 13 Dec 2006 09:22:17 +0000</pubDate>
		<guid>http://www.ismaelj.com/articulos/addevent-recoding-contest/#comment-390</guid>
					<description>That's simple: return &quot;false&quot; in the function you pass to &lt;code&gt;addEvent&lt;/code&gt;. The implementationt of &lt;code&gt;addEvent&lt;/code&gt; can't return a default value either true or false because is up to the developer to define the behavior of each event.</description>
		<content:encoded><![CDATA[	<p>That&#8217;s simple: return &#8220;false&#8221; in the function you pass to <code>addEvent</code>. The implementationt of <code>addEvent</code> can&#8217;t return a default value either true or false because is up to the developer to define the behavior of each event.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Andy</title>
		<link>http://www.ismaelj.com/articulos/addevent-recoding-contest/#comment-388</link>
		<pubDate>Tue, 12 Dec 2006 23:15:39 +0000</pubDate>
		<guid>http://www.ismaelj.com/articulos/addevent-recoding-contest/#comment-388</guid>
					<description>Hi, attaching an onclick to a form button won't correctly &quot;return false&quot;  to prevent the form submitting......what would you do to achieve this using addEvent()  ?

thx</description>
		<content:encoded><![CDATA[	<p>Hi, attaching an onclick to a form button won&#8217;t correctly &#8220;return false&#8221;  to prevent the form submitting&#8230;&#8230;what would you do to achieve this using addEvent()  ?</p>
	<p>thx
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Angel</title>
		<link>http://www.ismaelj.com/articulos/addevent-recoding-contest/#comment-363</link>
		<pubDate>Tue, 21 Nov 2006 14:53:12 +0000</pubDate>
		<guid>http://www.ismaelj.com/articulos/addevent-recoding-contest/#comment-363</guid>
					<description>ok muchas gracias</description>
		<content:encoded><![CDATA[	<p>ok muchas gracias
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Ismael Jurado</title>
		<link>http://www.ismaelj.com/articulos/addevent-recoding-contest/#comment-362</link>
		<pubDate>Tue, 21 Nov 2006 12:31:59 +0000</pubDate>
		<guid>http://www.ismaelj.com/articulos/addevent-recoding-contest/#comment-362</guid>
					<description>Precisamente ese es unos de los problemas a los que te enfrentas usando el codigo que muestras y una de las motivaciones de este artículo, que propone una implementación alternativa de la función addEvent</description>
		<content:encoded><![CDATA[	<p>Precisamente ese es unos de los problemas a los que te enfrentas usando el codigo que muestras y una de las motivaciones de este artículo, que propone una implementación alternativa de la función addEvent
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Angel</title>
		<link>http://www.ismaelj.com/articulos/addevent-recoding-contest/#comment-359</link>
		<pubDate>Tue, 21 Nov 2006 01:24:24 +0000</pubDate>
		<guid>http://www.ismaelj.com/articulos/addevent-recoding-contest/#comment-359</guid>
					<description>Hola a todos, me parece muy interesante el articulo, es bastante util pero no consigo
pasar el objeto a la funcion manejadora del evento con attachEvent, hay os dejo el codigo por si le quereis echar un vistazo:

function addEvent(obj,evType,evHandler){
	if(obj.addEventListener){
		obj.addEventListener(evType,evHandler,false)
		return true
	}
	else if(obj.attachEvent){
		var r = obj.attachEvent(&quot;on&quot; + evType,evHandler)
		return r;
	}
	else{
	return false;
	}
}

function prueba(e){
this.id //Funciona en mozilla...

//Aqui no consigo hacer referencia al objeto
}
var my_obj = document.getElementById(&quot;my_id&quot;)
addEvent(my_obj,'onmouseover',prueba);


Estaria muy agradecido si me pudieseis ayudar gracias.</description>
		<content:encoded><![CDATA[	<p>Hola a todos, me parece muy interesante el articulo, es bastante util pero no consigo<br />
pasar el objeto a la funcion manejadora del evento con attachEvent, hay os dejo el codigo por si le quereis echar un vistazo:</p>
	<p>function addEvent(obj,evType,evHandler){<br />
	if(obj.addEventListener){<br />
		obj.addEventListener(evType,evHandler,false)<br />
		return true<br />
	}<br />
	else if(obj.attachEvent){<br />
		var r = obj.attachEvent(&#8221;on&#8221; + evType,evHandler)<br />
		return r;<br />
	}<br />
	else{<br />
	return false;<br />
	}<br />
}</p>
	<p>function prueba(e){<br />
this.id //Funciona en mozilla&#8230;</p>
	<p>//Aqui no consigo hacer referencia al objeto<br />
}<br />
var my_obj = document.getElementById(&#8221;my_id&#8221;)<br />
addEvent(my_obj,&#8217;onmouseover&#8217;,prueba);</p>
	<p>Estaria muy agradecido si me pudieseis ayudar gracias.
</p>
]]></content:encoded>
				</item>
</channel>
</rss>
