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

iterating thru all combos on page and getting selected text

Status
Not open for further replies.

MontyBurns

Programmer
Oct 3, 2001
99
GB
Hi,

I have some combos which are being dynamically added to the page (i.e. multiple dependent combos) and I never know how many might be on the page.

All is working fine, except I need to get the actual text of each selected option, for each combo that exists on the page. I'm almost there - I just can't seem to figure out the correct syntax to retrieve the text. I can get the value attribute no probs like below:

var combos = document.getElementsByTagName("select");

for(var counter=0; counter < combos.length;counter++)
{
alert(combos.item(counter).value);
}

If I try replacing .value with .text it simply alerts &quot;undefined&quot;.

Any ideas anyone? Any pointers would be really appreciated.

Thanks,
Monty
 
this is what i use for <SELECT> input. I think it uses
the same method. give it a try:-

//to get the values
alert(myListBox.options[myListBox.length-1].value );

//to get the text
alert(myListBox.options[myListBox.length-1].text = &quot;text&quot;);
 
opp. selected text? here it is:-

combos.options[combos.selectedIndex].value;
 
Max - i'm getting the following error:

'options' is null or not an object

Any ideas?

Thanks for the reply,
Monty
 
i've managed to figure it out myself - it doesn't look too pretty, but it works!

combos(counter).options[combos(counter).selectedIndex].text

Thanks for putting me on the right road Max.

Monty
 
Umm.. somehow i don't get notification when you reply!

I get &quot;null or not an object&quot; error a lot when I use dot notation. Hence, I always use this method(works like a charm everytime):-

if you have an input box with id = &quot;txtInput&quot;, i.e

<input id=&quot;txtInput&quot;>

using javascript, I recommend use this method to get a reference to the object:-
var sTemp = document.getElementById(&quot;txtInput&quot;);

Remember, this is an object so to retrieve the value,
you have to use sTemp.data.You can also check the tag type by saying, sTemp.tag. Tag could be &quot;INPUT&quot;, &quot;SELECT&quot; etc.

debugging trick
----------------
The trick to see what element an object has is by getting into the debug mode on MS Interdev. The dirty trick? Create and refer to an UNDEFINED variable on the page using some dummy statement that force the browser to generate some errors like, &quot;Syntax Error found. Do you wish to debug?&quot;

Click Yes, and choose Interdev(if you use one). Then
in Interdev, under quick watch, type in sTemp(the var mentioned above). This should retrieve all the elements that sTemp has so you can see what they all represent.

Coding can get messy in ASP/javascript. That's why I design my app so it does not have any database objects etc and with very little business rules. All reside in the DLLs that are ran by COM+.






 
thanks for the tips. I've never really known how to debug JS apart from using alert etc.

It tends tp p!ss me off when I choose to debug, enter Interdev and then not know what i'm doing! It also p!sses me off that when you close that instance of Interdev it closes the browser window that you were working with - can this be avoided?

Yeah, i'd much prefer to do things that way, but this app just doesn't warrant the dev time to do all the DLLs etc. Just waiting on our firm to start using .Net - should be a new challenge...

Thanks again,
Monty
 
MontyBurns,

to exit debug mode in interdev without closing the attached browser, click Debug > Detach all processes.

=========================================================
while (!succeed) try();
-jeff
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top