Jump to content

JS: variable.id = "" does not work in IE


tinfanide
 Share

Recommended Posts

<!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"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Untitled Document</title><script>window.onload = function(){	// div1	var div1 = document.getElementById('div1');	var a = document.createElement('a');	var txt = document.createTextNode('This is a link');		a.appendChild(txt);	div1.appendChild(a);		a.setAttribute('href','http://www.google.com/');// div2	var div2 = document.getElementById('div2');	div2.innerHTML = "<a href='http://www.google.com/'>Google</a>";// div3	var newdiv = document.createElement('div');	newdiv.id = 'div3';	document.body.appendChild(newdiv);	div3 = document.getElementById('div3');	div3.innerHTML = "div3"; //IE does not show it up	}</script></head><body><div id="div1"></div><div id="div2"></div></body></html>

Div1 & Div2 work perfectly well in both IE and Firefox.But how about Div3 where I used createElement and .id of JS?It works in Firefox but not IE.Is there anything wrong with my script?

Link to comment
Share on other sites

1. When you first declare a variable (when you use it the first time) use the var keyword.2. Older versions of IE did not work correctly if the name of a variable was the same as the id of an element. Try naming the variable something else.

Edited by Deirdre's Dad
Link to comment
Share on other sites

1. When you first declare a variable (when you use it the first time) use the var keyword.2. Older versions of IE did not work correctly if the name of a variable was the same as the id of an element. Try naming the variable something else.
Yes, good for the remark.My IE8 cannot recognise a first-declared variable without "var" but still works well when it reads a variable with the same name to the id.
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...