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!

Using Split to create an array 1

Status
Not open for further replies.

mickeyg

Technical User
Mar 30, 2001
120
US
I am trying to loop through a combo box and create an 3 component array that I will later use to dynamically rebuild the combo box based on another combo box value.

I am not having success using the split command.
Here is my code:
<form>
<table width=&quot;300&quot; class=&quot;formdata&quot; cellspacing=&quot;0&quot;>
<tr valign=&quot;bottom&quot;>
<th colspan=&quot;2&quot; class=&quot;titleheadersub&quot;>Enter Product Information</th>
</tr>
<tr>
<td class=&quot;label&quot;>Product:</td>
<td> <select name=&quot;publication&quot;>
<option value=&quot;GZ&quot; >Daily Gazette</option>
<option value=&quot;DM&quot; >Daily Messenger</option>
</select>
</td>
</tr>
<tr>
<td class=&quot;label&quot; nowrap>Schedule:&nbsp;</td>
<td> <select name=&quot;schedule&quot;>
<option value=&quot;DM,DS&quot;>Daily and Sunday</option>
<option value=&quot;GZ,ds&quot;>Daily and Sunday</option>
<option value=&quot;DM,DS&quot;>Daily and Sunday</option>
<option value=&quot;DM,DO&quot;>Daily Only (Monday to Friday)</option>
<option value=&quot;DM,ANYTIME&quot;>DELIVERY ANY GIVEN DAY</option>
<option value=&quot;DM,ANYTIME&quot;>DELIVERY FOR ANY GIVEN DAY</option>
<option value=&quot;GZ,ANYTIME&quot;>DELIVERY FOR ANY GIVEN DAY</option>
<option value=&quot;DM,alldays&quot;>EVERY DAY</option>
<option value=&quot;DM,MWFMIXED&quot;>MWF MIXED DELIVERY - WED RTE</option>
<option value=&quot;DM,SS&quot;>SAT/SUN</option>
<option value=&quot;DM,SO&quot;>SUNDAY ONLY</option>
<option value=&quot;GZ,SO&quot;>SUNDAY ONLY</option>
<option value=&quot;DM,TUEMON&quot;>TUEMON</option>
<option value=&quot;DM,TuesONLY&quot;>Tuesday Only</option>
<option value=&quot;DM,MAILDS&quot;>US MAIL DAILY & SUNDAY</option>
<option value=&quot;GZ,MAILDS&quot;>US MAIL DAILY & SUNDAY</option>
<option value=&quot;DM,MAILDO&quot;>US MAIL DAILY ONLY</option>
<option value=&quot;GZ,MAILDO&quot;>US MAIL DAILY ONLY</option>
<option value=&quot;DM,MAILSO&quot;>US MAIL SUNDAY ONLY</option>
<option value=&quot;GZ,MAILSO&quot;>US MAIL SUNDAY ONLY</option>
</select> </td>
<script language=&quot;JavaScript&quot; type=&quot;text/JavaScript&quot;>
// create array
function combo(product, schedule, desc) {
this.product = product;
this.schedule = schedule;
this.desc = desc
}
// loop through combo box and build array
for (var i = 0; i < document.forms[0].schedule.length; i++) {
var myString = document.forms[0].schedule.value;
//parse out comma separated string
var myNewstring = myString.split(&quot;,&quot;);
var myArray = new Array(document.forms[0].schedule.value);
// productID, scheduleID, text description
// desired result: DM, DS, Daily and Sunday
var mycombo = new combo(myNewstring[1], myNewstring[2], document.forms[0].schedule.text)
}
</script>
</tr>
</table>
</form>

Any assistance is greatly appreciated!
Thanks,
Mickey
 
Try this script...
Code:
for (var i = 0; i < document.forms[0].schedule.options.length; i++) {
    var myString = document.forms[0].schedule.options[i].value;
    //parse out comma separated string
    var myNewstring = myString.split(&quot;,&quot;);
    // productID, scheduleID, text description
    // desired result: DM, DS, Daily and Sunday
    alert(myNewstring[0] + '\n' + myNewstring[1] + '\n' + document.forms[0].schedule.options[i].text);
}
 
In Netscape 6.2 my &quot;rebuilt&quot; combo box is empty.
Any ideas?

function rebuildsched() {
var j = 0;
var myform = document.forms[0];

// clear combo box
myform.schedule.length = 0;

// loop through array to find matching entries
for (var i = 0; i < schedulecombo.length; i++) {

// add combo box entry
if (myform.publication.value == schedulecombo.product) {
var j = j + 1;
// this is not working in Netscape
myform.schedule.options[j] = new Option(schedulecombo.desc);
myform.schedule[j].value = schedulecombo.schedule;
}
}
}

Thanks,
Mickey
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top