Jump to content

Event Listeners In Ie Don't Work


MrFish
 Share

Recommended Posts

I'm making custom events in javascript and they won't work in IE. This is the javascript specific for the listeners-

function addEventListener(obj, eventType, listener, capturing)		{			if(obj.addEventListener)			{				obj.addEventListener(eventType, listener, capturing);			}			else if(obj.attachEvent)			{				obj.attachEvent("on" + eventType, listener);			}		}

And here is the full code if you want to try it

<html><head>	<title>Event Tests</title>	<script type="text/javascript">		function addEventListener(obj, eventType, listener, capturing)		{			if(obj.addEventListener)			{				obj.addEventListener(eventType, listener, capturing);			}			else if(obj.attachEvent)			{				obj.attachEvent("on" + eventType, listener);			}		}				function Events()		{			var draggable = document.getElementsByClassName("draggable");			for(var x in draggable)			{				addEventListener(draggable[x], "mousedown", drag, true);				addEventListener(draggable[x], "mouseup", drop, false);			}		}				// Listeners			function drag()			{				this.innerHTML += "1";			}						function drop()			{				this.innerHTML = "dropped";			}		//				function init()		{			Events();		}	</script>	<style type="text/css">		div		{			float: left;			margin: 10px;			background-color: #0ff;			width: 100px;			height: 100px;			text-align: center;		}				.draggable		{			background-color: #ff0;			pointer; move;		}	</style></head><body onload="init()">	<div></div>	<div></div>	<div class="draggable"></div>	<div></div></body></html>

Anyone know what's up?

Link to comment
Share on other sites

IE do not support getElementsByClassName you can use instead getElementsByTagName, getElementsById and getElementsByName

I'm making custom events in javascript and they won't work in IE. This is the javascript specific for the listeners-
function addEventListener(obj, eventType, listener, capturing)		{			if(obj.addEventListener)			{				obj.addEventListener(eventType, listener, capturing);			}			else if(obj.attachEvent)			{				obj.attachEvent("on" + eventType, listener);			}		}

And here is the full code if you want to try it

<html><head>	<title>Event Tests</title>	<script type="text/javascript">		function addEventListener(obj, eventType, listener, capturing)		{			if(obj.addEventListener)			{				obj.addEventListener(eventType, listener, capturing);			}			else if(obj.attachEvent)			{				obj.attachEvent("on" + eventType, listener);			}		}				function Events()		{			var draggable = document.getElementsByClassName("draggable");			for(var x in draggable)			{				addEventListener(draggable[x], "mousedown", drag, true);				addEventListener(draggable[x], "mouseup", drop, false);			}		}				// Listeners			function drag()			{				this.innerHTML += "1";			}						function drop()			{				this.innerHTML = "dropped";			}		//				function init()		{			Events();		}	</script>	<style type="text/css">		div		{			float: left;			margin: 10px;			background-color: #0ff;			width: 100px;			height: 100px;			text-align: center;		}				.draggable		{			background-color: #ff0;			pointer; move;		}	</style></head><body onload="init()">	<div></div>	<div></div>	<div class="draggable"></div>	<div></div></body></html>

Anyone know what's up?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...