Jump to content

Problem with IE: working with DOM


tgupta@fidelia.com
 Share

Recommended Posts

Hi    I hava prepaired a select box using Dom. But when I am firing a onClick event on Firefox it is working fine. The same code is not working on IE 6.0.

Hi here is the code:
<HTML><head><script language="javascript">function addRow(){	var selectBox = document.createElement("select");	selectBox.setAttribute("id", "selectBox");	selectBox.setAttribute("onChange", "alert(\'Hello God\')");		var optionOne = document.createElement("option");	var txt1 = document.createTextNode("One");	optionOne.appendChild(txt1);	var optionTwo = document.createElement("option");	var txt2 = document.createTextNode("Two");	optionTwo.appendChild(txt2);	var optionThree = document.createElement("option");	var txt3 = document.createTextNode("Three");	optionThree.appendChild(txt3);	selectBox.appendChild(optionOne);	selectBox.appendChild(optionTwo);	selectBox.appendChild(optionThree);	var customTr = document.createElement("tr");	var customTd = document.createElement("td");	customTd.appendChild(selectBox);	customTr.appendChild(customTd);	var trNode = document.getElementById("tr1");	var tableNode = trNode.parentNode;	tableNode.insertBefore(customTr,trNode);}</script></head><BODY><form><table id="table" border="1"><tr id="tr1"><td><input type="button" id="button1" value="Add Select Box" onClick="addRow()"></td></tr></table></form></BODY></HTML>

Link to comment
Share on other sites

You will have to figure out how you want to test browsers.Opera (version 8.5) now passes the document.all test so I threw in a userAgent test here:Anyway here is a reference regarding IE's method to "createElement":createElementand attachEvent:attachEvent

<HTML><head><script type="text/javascript">function addRow(){if(window.attachEvent && navigator.userAgent.indexOf('Opera') == -1){var selectBox = document.createElement('<select name="selectBox" id="selectBox" onChange="show(this)">');}else {var selectBox = document.createElement("select");selectBox.setAttribute("name", "selectBox");selectBox.setAttribute("id", "selectBox");selectBox.setAttribute("onChange", "show(this)");}var optionOne = document.createElement("option");var txt1 = document.createTextNode("One");optionOne.appendChild(txt1);var optionTwo = document.createElement("option");var txt2 = document.createTextNode("Two");optionTwo.appendChild(txt2);var optionThree = document.createElement("option");var txt3 = document.createTextNode("Three");optionThree.appendChild(txt3);selectBox.appendChild(optionOne);selectBox.appendChild(optionTwo);selectBox.appendChild(optionThree);var customTr = document.createElement("tr");var customTd = document.createElement("td");customTd.appendChild(selectBox);customTr.appendChild(customTd);var trNode = document.getElementById("tr1");var tableNode = trNode.parentNode;tableNode.insertBefore(customTr,trNode);}function show(el){  alert(el.options[el.selectedIndex].text);}</script></head><BODY>test<form name="test" method="get"><table id="table" border="1"><tr id="tr1"><td><input type="text" value="test" name="t"><input type="button" id="button1" value="Add Select Box" onClick="addRow()"><input type="submit"></td></tr></table></form>test</BODY></HTML>

Good Luck, :)

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