Jump to content

Update HTML DOM Tutorial

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


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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...