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

PLEASE HELP! Listbox Items Retrieval

Status
Not open for further replies.

behbeh

Programmer
Mar 10, 2001
48
US
Hi!
I'm new to JavaScript and I have been painstakingly searching for info on how to grab all the items (values) in a listbox. The issue is that I need ALL the values; I'm using a calendar control's click event to populate days selected into a listbox. On form submit, I need to grab all of the dates in the listbox...most likely they will not be selected...it's just a 'container' to hold the values (add and remove dates).
If anyone can help me I'd GREATLY appreciate it!!!!
 
As far as I know there is no one line code which will give you the values of all items in a list box. You have to loop through all the items and get the values of individual items in some temporary variable.

var strValues = '';
var strText = ''
for var i=0, i<document.formname.lstboxname.options.length;i++
{
strValues += document.formname.lstboxname.options.value + 'some delimeter';
//strValues += document.formname.lstboxname.options.text + 'some delimeter';
}
If you want to get the item descriptions which are displayed in the listbox then you can use &quot;text&quot; instead of &quot;value&quot;
 
got bit by the i bug [lol] Not everything you type is understanding to others! Think about that before getting upset with a wrong answer to your question.

admin@onpntwebdesigns.com
 
if you want to access all of the values in a listbox once the form is submitted, and you don't care which (if any) of the values the user had selected (i.e., a container as you described), consider automatically selecting every value as the form is submitted. that way, all values will be available in the Request.Form object on your next page. i had same situation where i wasn't getting all of the values in a listbox and realized they must all be selected to be passed along.

function selectAllOptions(obj) {
for (var i=0; i<obj.options.length; i++) {
obj.options.selected = true;
}
}

function submitIt(theForm) {
selectAllOptions(theForm.myListBox);
theForm.submit();
}

<form name=&quot;frmMain&quot; method=&quot;post&quot; onSubmit=&quot;submitIt(this);&quot;>
... your form stuff ...
</form>



(p.s., what is up with the automatic italics?)
 
Thanks!!!!!!!
I was able to make it work (with the help of a hidden input box) once I selected all of the options
:)
Thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top