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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

if select only has one option, select it

Status
Not open for further replies.

Tracey

Programmer
Oct 16, 2000
690
NZ
can someone help me with this please?

I am dynamically changing the size of a <select> depending on how many options:
Code:
for (var i=0; i < objNodeList.length; i++) {

  bob.comboResults.options[i]=new Option(objNumberList.item(i).text + ' - ' + objNodeList.item(i).text, objindex.item(i).text);
  bob.comboResults.size=i+1;
 }

however, if there is only one node in objNodeList, the select sizes to 1, but you cant see the record selected.

how can i say &quot;if objNodeList.length = 1 {selectit}&quot;??

i tried:
[code]
if (objNodeList.length = 0) {
document.getElementById(&quot;comboresults&quot;).selectedindex=document.getElementById(&quot;comboresults&quot;).options.length;}
but this doesnt work.

Trying to learn Javascript in notepad is a pain in the ...



Tracey
Remember... True happiness is not getting what you want...

Its wanting what you have got!
 
oops
i mean
if (objNodeList.length = 1)

and i tried

{document.getElementById(&quot;comboresults&quot;).selectedindex=i+1;}

too

Tracey
Remember... True happiness is not getting what you want...

Its wanting what you have got!
 
how bout
if (objNodeList.length == 1) {
document.getElementById(&quot;comboresults&quot;).selectedindex=document.getElementById(&quot;comboresults&quot;).options.length;}


_____________________________________________________________________
onpnt2.gif

Hakuna matata!!
 
nope, doesnt work

Tracey
Remember... True happiness is not getting what you want...

Its wanting what you have got!
 
if (objNodeList.length == 1)
document.getElementById(&quot;comboresults&quot;).options[0].selected = true;

should work.
 
Tracey,

I may have misunderstood your problem, so please ignore this if I have ;o)...

From what I understand, when your dynamically loaded SELECT box only has 1 item, you can't see it, and thus you want to automatically select it.

I couldn't recreate the problem where a 1-item select box didn't show the item, but have a look at this code anyway:

Code:
<HTML>
<HEAD>

<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
<!--

	var formObj;
	var objNodeList = ['Option 1', 'Option 2', 'Option 3', 'Option 4', 'Option 5', 'Option 6'];

	function loadItems(numItemsToLoad)
	{
		for (var i=0; i < numItemsToLoad; i++) formObj.comboResults.options[i] = new Option(objNodeList[i], objNodeList[i]);
		formObj.comboResults.size = numItemsToLoad;
		if (numItemsToLoad == 1) formObj.comboResults.selectedIndex = 0;
	}

	function setupVars()
	{
		formObj = document.forms['myForm'];
		loadItems(6);
	}

//-->
</SCRIPT>

</HEAD>
<BODY onLoad=&quot;setupVars();&quot;>

<FORM NAME=&quot;myForm&quot;>
	<SELECT NAME=&quot;comboResults&quot; MULTIPLE></SELECT>
</FORM>

</BODY>
</HTML>

When run, the code creates a 6-item select box from an array. If you chaneg line 20 to read &quot;loadItems(1);&quot; and refresh your page, you'll see a 1-item select box with the item highlighted.

Does this help at all, or did I really misunderstand your post?

Dan
 
ah, and the reason why onpnt's answer doesnt work, is that the index of the select option is the primary key of the record. This is held in objindex.

however, i get an error on ths:
if (objNodeList.length == 1) {document.getElementById(&quot;comboResults&quot;).selectedindex=objindex.item.text;}

saying &quot;wrong number of arguments or invalid property assignment&quot;

what's that mean? What am i missing?





Tracey
Remember... True happiness is not getting what you want...

Its wanting what you have got!
 
Can you cheat and pasteHTML or replace it with innerHTML.

<span id=&quot;containsMyMenu&quot;><input type=&quot;&quot;></span>

if (objNodeList.length == 1) {document.getElementById(&quot;containsMyMenu&quot;).innerHTML='<input type=&quot;&quot;>';}

Sadly it's stuffed with NS4 or older, I think :(

If I understand correctly, it's the same problem as trying to dynamically check a checkbox. Just doesn't work - at least not for me :p

It's like <input type=&quot;checkbox&quot; checked> but 'checked' is not an attribute so it cannot be toggled by a script :/

----------
I'm willing to trade custom scripts for... [see profile]
 
ah, i have it
Code:
     if (objNodeList.length == 1) {document.bob.comboResults.selectedIndex=objindex.item(i).value;}
or
Code:
     if (objNodeList.length == 1) {document.getElementById(&quot;comboResults&quot;).selectedIndex=objindex.item(i).value;}


it was .value i wanted (and mental note to use () not [])
banghead.gif






Tracey
Remember... True happiness is not getting what you want...

Its wanting what you have got!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top