Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

weird behavior w/ innerHTML and select tag 1

Status
Not open for further replies.

gacaccia

Technical User
May 15, 2002
258
0
0
US
hi all,

i'm trying to do something that i assume is common, to have a select control with a list of options, and then to update that list of options based on user action.

my problem is, i'm using innerHTML to initialize and change the list of options, but for some reason the first &quot;<option value='1'>&quot; part of the new HTML is getting cut off.

here is the section of code for changing it. any thoughts? uList is the id for the HTML select tag. uList is also the name of a javascript array of objects with member properties including &quot;sName&quot;.

Code:
function initList() {
	 document.getElementById(&quot;uList&quot;).innerHTML = &quot;&quot;;
	 for (i=0;i<uList.length;i++) {
			document.getElementById(&quot;uList&quot;).innerHTML += &quot;<option value='&quot; + i + &quot;'>&quot; + uList[i].sName + &quot;</option>&quot;;
	 }
}

if the array of uList.sName's include &quot;zero&quot; and &quot;one&quot;, then the desired result for innerHTML would be...

Code:
<option value='0'>zero</option><option value='1'>one</option>

instead, i'm getting...

Code:
zero</option><option value='1'>one</option>

thanks,

glenn
 
as a follow up to my first post, i thought i'd make a test page to illustrate the problem. here's a complete html page that exhibits the problem.

Code:
<html><head><title>Test</title>
<script language=&quot;JavaScript&quot; type=&quot;text/javascript&quot;>
<!--
list = new Array(&quot;zero&quot;,&quot;one&quot;,&quot;two&quot;);

function initList() {
		document.getElementById(&quot;list&quot;).innerHTML = &quot;&quot;;
		for (i=0;i<list.length;i++) {
				document.getElementById(&quot;list&quot;).innerHTML += &quot;<option value='&quot; + i + &quot;'>&quot; + list[i] + &quot;</option>&quot;;
		}		
		alert(document.getElementById(&quot;list&quot;).innerHTML);		 
}
//-->
</script>
</head><body onload=&quot;initList()&quot;>
<select id=&quot;list&quot;></select>
</body></html>
 
that would certainly work for initializing the select control, but would not work for changing the list of options based on user input after the page had loaded. the above function, slightly modified, is what i want to use for changing the list of options in the select control.

besides, innerHTML is basically working, it's just skipping the first tag (in IE 6.0). anyone know why? am i doing something wrong or is this a bug?
 
gacaccia,

you should be modifying a <select> list's options by creating new Option objects:

<html>
<head>
<script type=&quot;text/javascript&quot;>
function addOption() {
var oSel = document.forms[0].sel;
var oOpt = new Option(&quot;Opt_&quot; + oSel.options.length, oSel.options.length);
oSel.options[oSel.options.length] = oOpt;
}
</script>
</head>
<body>
<form>
<select name=&quot;sel&quot;></select>
<input type=&quot;button&quot; value=&quot;add&quot; onclick=&quot;addOption();&quot;/>
</form>
</body>
</html>


=========================================================
try { succeed(); } catch(E) { tryAgain(); }
-jeff
 
thanks for the response. i hadn't realized that method. still would like to know why innerHTML was acting so strange, but as long as there is a way to get the job done.

i actually ended up using the DOM methods for adding and removing nodes for option tags, though your solution looks simpler than what i did.

here are my finished functions for initializing, clearing and setting a select control.

Code:
function initList() {
	  clearList();
    for (i=0;i<uList.length;i++) {
	    	var option;
				var optionText;
				option = document.createElement(&quot;option&quot;);	 
				optionText = document.createTextNode(uList[i].sName);
				option.appendChild(optionText);
				option.setAttribute(&quot;value&quot;,i);
				document.getElementById(&quot;uList&quot;).appendChild(option);
		} 
}

function setList(indexes) {
 		clearList();
    for (i=0;i<indexes.length;i++) {
	    	var option;
				var optionText;
				option = document.createElement(&quot;option&quot;);	 
				optionText = document.createTextNode(uList[indexes[i]].sName);
				option.appendChild(optionText);
				option.setAttribute(&quot;value&quot;,indexes[i]);
				document.getElementById(&quot;uList&quot;).appendChild(option);
		} 
}

function clearList() {
	 var done;
	 var obj;
	 done = false;
	 obj = document.getElementById(&quot;uList&quot;);
	 while (done == false) {
	 		if (obj.hasChildNodes() == true) {
				obj.firstChild.removeNode(true);
			} else {
				done = true;
			}
	 }
}
 
imho, why would you want to use anything other than the Option object? it's DOM (cross-browser) and it's very easy to use.



=========================================================
try { succeed(); } catch(E) { tryAgain(); }
-jeff
 
I had a similar problem. I assume it is because the <OPTION> tag is a part of the <SELECT> tag though that doesnt really explain why innerhtml behaves the way it does. What i did was create a string with all my options in it and then use &quot;outerHTML&quot; and reprint the <SELECT> tags concatenated with the options string.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top