Jump to content

Undefined target in prior versions of IE


chrici

Recommended Posts

I have the following code.

<!DOCTYPE html><html>	<body>		<p id="clickme">Click me!</p>		<div id="new"></div>		<script type="text/javascript">		document.onclick = function(event) {			target = event.target || event.srcElement;						if (target.id == 'clickme') {				paragraph = document.createElement('p');				content = document.createTextNode('This is a test...');								paragraph.appendChild(content);				document.getElementById('new').appendChild(paragraph);			}		}		</script>	</body></html>

When the paragraph id="clickme" is clicked a child paragraph should be appended to id="new". This works in all browsers except for prior versions of IE than 9. I do not know which prior versions it is since I am using the compability mode in IE9 to test. Using Firebug Lite for IE I receive this error when id="clickme" is clicked:"Unable to get value of the property 'target': object is null or undefined"I don't know how to fix this error and why it occurs. Please help me. :)

Link to comment
Share on other sites

The parameter called "event" is overriding the window.event object for older Internet Explorer versions.Try this:

function(e) {  e = e ? e : window.event;  target = e.target || e.srcElement;

Link to comment
Share on other sites

The parameter called "event" is overriding the window.event object for older Internet Explorer versions.Try this:
function(e) {  e = e ? e : window.event;  target = e.target || e.srcElement;

I tried to do it but it is not working.I am not passing anything into the functions event parameter. I am just doing what I have seen other sites do.
Link to comment
Share on other sites

Out of curiosity, why not bind the click handler to the <p> element itself? The times when I've wanted a click handler assigned to the entire window are extremely rare and very specialized.

Link to comment
Share on other sites

Because that I am the kind of person who learns by trying. This code is not for a client or a site.I read about event targeting some time ago and thought that I would like to learn it :)

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...