Jump to content

Update HTML DOM Tutorial


jesh
 Share

Recommended Posts

It had been a few months since I was faced with dynamically creating a drop down menu using the DOM so I turned (as I always do) to the tutorials on the site - specifically here:http://www.w3schools.com/htmldom/met_select_add.aspThe solution, as it is presented, doesn't work cross-browser. I suggest updating it to something like this:BEFORE:

function insertOption()  {  var y=document.createElement('option');  y.text='Kiwi'  var x=document.getElementById("mySelect");  try	{	x.add(y,null); // standards compliant	}  catch(ex)	{	x.add(y); // IE only	}  }

AFTER:

function insertOption(){	var y = document.createElement('option');	y.appendChild(document.createTextNode('Kiwi'));	var x = document.getElementById("mySelect");	x.appendChild(y);}

This way it'll work, at least, on the PC using Firefox 2.0, IE 6.0, Opera 9.1 and on the Mac using Firefox 1.5 and Safari (didn't check the version). The solution, as currently presented, doesn't work at all in Safari.

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...