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!

Array for Select box and value questions 1

Status
Not open for further replies.

lesleint

IS-IT--Management
Apr 11, 2003
144
US
I have this so far


And this only sets options in the select and I would like to have the option be the text but I want to make the value something different

Thanks for your help
 
[tt]
// Now loop through all the elements of the passed array to populate the second combo
for (count=0;count<mySitestext.length;count++){
document.caseLog.PROCCODE.options[count].text=mySitestext[count];
document.caseLog.PROCCODE.options[count].value=mySitesvalue[count];
}
[/tt]

[sub]Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
[/sub]
 
Thank you sir and your quote is divine!
If you can I have another question concerning this script. On my select list I must have at least 24 options like this

<option> </option>

or it does not work. Is there a way to just have it dynamically create those. It is not bad but it leaves alot of blank options at the bottom when my array is smaller than 24
 
Ah yes... I did think you might run into problems with the way you had things, but figured to let sleeping dogs lie if they weren't causing you any problems. But since you bring the subject up:

Your Code:
[tt]
// First clear the current entries in the second combo box

for (count=document.caselog.proccode.options.length-1;count>0;count--){
document.caseLog.PROCCODE.options[count].text=&quot;&quot;;
}
[/tt]

Doesn't actually remove the options from the select. It merely deletes the text out of them. Similar to if you delete the text out of a dozen text files, you still have the empty shells sitting there.

The preferred way of cleaning out a select list:
[tt]
while(document.caselog.proccode.options.length != 0){
document.caseLog.PROCCODE.options[count] = null;
}
[/tt]

Similarly, the code:
[tt]
// Set first entry in the second combo, this is displayed initially

//document.caseLog.PROCCODE.options[0].text = &quot;Select a Proc Code&quot;;

// Now loop through all the elements of the passed array to populate the second combo
for (count=0;count<mySitestext.length;count++){
document.caseLog.PROCCODE.options[count].text=mySitestext[count];
}
[/tt]
Doesn't actually create new options, it merely overwrites the text attributes of the options that are already there:

Preferred method:
[tt]
// Set first entry in the second combo, this is displayed initially

//document.caseLog.PROCCODE.options[0] = new Option(&quot;Select a Proc Code&quot;);

// Now loop through all the elements of the passed array to populate the second combo
for(var count=0;count<mySitestext.length;count++){
var insertionIndex = document.caseLog.PROCCODE.options.length;
document.caseLog.PROCCODE.options[insertionIndex] = new Option(mySitestext[count]);
}
[/tt]

Doing things this way will ensure that you only ever create the options that you need, rather than having to have a stack of empty options.

[sub]Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
[/sub]
 
Thanks a lot, being fairly new to this and trying to convert an Access Application to the web gets a little crazy but people like you are the reason I keep going and learning.
 
dwarfthrower the code works if there is not options at all on the PROCCODE select but once it gets populated then it will lock up IE
 
OK Fixed that but how do I add values to the select now with your code?
Code:
<script language=&quot;JavaScript&quot;>
function selectcategory(){

var num=document.caseLog.LEVEL.value;

if (num=='FSW') {populateCategory(FSWText);}
if (num=='BACHELOR') {populateCategory(BachelorText);}
if (num=='MASTER') {populateCategory(MasterText);}
}

function populateCategory(mySitestext){

while(document.caseLog.PROCCODE.options.length != 0){
 document.caseLog.PROCCODE.options.length = 0;
 document.caseLog.PROCCODE.options[count] = null;
}
for(var count=0;count<mySitestext.length;count++){
 var insertionIndex = document.caseLog.PROCCODE.options.length;
 document.caseLog.PROCCODE.options[insertionIndex] = new Option(mySitestext[count]);
 //document.caseLog.PROCCODE.options[insertionIndex].value = Option(mySitesvalue[count]);
}

// Set the first entry as selected
document.caseLog.PROCCODE.options[0].selected=true;
window.document.getElementById('divProCode').style.display = 'block';
}
</script>
 
Well, first you'll need to set up some arrays to hold the values, just as you've done with the text (presuming you've done that, just didn't see it in the code)

Then it would be as you have it above, but without the // comment in front of the line.
[tt]
document.caseLog.PROCCODE.options[insertionIndex].value = Option(mySitesvalue[count]);
[/tt]

Where mySitesvalue is the array of values that match the mySitestext entries.

[sub]Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
[/sub]
 
Terribly sorry, wasn't concentrating on that post:

document.caseLog.PROCCODE.options[insertionIndex].value = mySitesvalue[count];

No 'Option()' needed here as you are simply setting the value property of an existing Option object.


[sub]Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
[/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top