/    Sign up×
Community /Pin to ProfileBookmark

Hi
I defined a new array called “arrStgy” which gets all strategy names from xml doc. This is a one dimensional array with strategy name.
But I want to populate my select box with a two dimensional array with both values being same for option text and option value.
My code does not work now because I have a one dimensional array.
How can i do this?
arrStgy = new Array();
var i = 0;

<xsl:for-each select=”//Class/Master/Strategies”>
arrStgy[i] = ‘<xsl:value-of select=”name”/>’
i = i + 1;
</xsl:for-each>

function getStgy()
{
ccySelected = document.forms[0].Id_ccy[document.forms[0].Id_ccy.selectedIndex].text;
n = new Array();

strategy = new Array();
var count = 0;
for(var j=0; j &lt; arrStgy.length; j++)
{
var strStgy = arrStgy[j];

var strCurrency = strStgy.substring(0,strStgy.indexOf(“#”));

var strStgyCcy = strStgy.substring(strStgy.indexOf(“#”)+1);

if(ccySelected == strCurrency)
{
strategy[count] =strStgyCcy;
count++;
ccyStgy = (strStgy);
fill( form.selectbox, ccyStgy );
}
}
}

function fill( sel, ar )
{
var ix;
for ( ix = sel.options.length-1; ix >= 0; –ix )
sel.options[ix] = null;
for ( ix = 0; ix &lt; ar.length; ++ix )
sel.options[ix] = new Option( ar[ix][0], ar[ix][1] );
}

to post a comment
JavaScript

12 Comments(s)

Copy linkTweet thisAlerts:
@Khalid_AliDec 20.2003 — the first thing u must verify that if array is being created by XSL
Copy linkTweet thisAlerts:
@miliauthorDec 20.2003 — Khalid,

Yes, at first I loop thro the xml and store all the values in a array.The values in array I have are the format

"USD#blahblah", "EUR#blahblah", "TKY#blahblah"


I have a static drop down with option values USD,TKY etc.This drop down is named Id_ccy.I want to populate another dropdown box called selectbox, based on the value selected from Id_ccy.

Now using JS i'm looping thro the array,trying to split the data before and after #.This gives me a list of all values befor and after #.

If my Id_ccy value selected = the value before #, I want to populate the corresponding data in selectbox.

I hope I'm clear.

Thanks
Copy linkTweet thisAlerts:
@miliauthorDec 20.2003 — If both my option value and option text are same, I guess I could say

sel.options[ix] = new Option( ar[ix], ar[ix]); in fill();

But this doesnt seem to work as well.
Copy linkTweet thisAlerts:
@Khalid_AliDec 20.2003 — May be [url=http://www.webapplikations.com/pages/html_js/forms/DropDownSelectCountryShowCities.html] This link will shed some light[/url] on how to populate the second list box
Copy linkTweet thisAlerts:
@miliauthorDec 20.2003 — Khalid, that for the link.I went thro' the code.

But I dont understand something.....

I'm trying to parse my array value to separate values before & after the delimeter #

My guess was that, in the function, fill, I try to treat that second argument as an array. it doesn't work, because it isn't an array, at all.

So I changed my fill() to

sel.options[ix] = new Option(ar,ar);

This time, it loads the selectbox, but only the last value from my array list.

I dont know where I'm going wrong.
Copy linkTweet thisAlerts:
@Khalid_AliDec 20.2003 — I hope this will help

<script type="text/javascript">

<!--

function Process(){

var arrStrgy = new Array("USD#blahblah", "EUR#blahblah", "TKY#blahblah");//assuming you have correctly created array

//now you want to split the array by # mark

//we will do that and create 2 arrays one with the value b4 the # and one with value after

var firstValArr = new Array();

var secValArr = new Array();

for(var x=0;x<arrStrgy.length;x++){

var temp = arrStrgy[x].split("#");

firstValArr[x] = temp[0]; //value before #

secValArr[x] = temp[1]; //value after #

}

alert("BEFORE ["+firstValArr+"]nAFTER ["+secValArr+"]")

}

//-->

</script>

</head>

<body>

<form id="form1" action="" onsubmit="">

<input type="button" value="process" onclick="Process()"/>

</form>
Copy linkTweet thisAlerts:
@miliauthorDec 20.2003 — Thanks,khalid. That worked. I could see the values in my alert.I didnt know that i could use "split".

How can I check if ccyselected == all the values firstValArr[x]

and load only the related secValArr[x] in my selectbox.

In your example in the link you sent me, you have if selection==locArr[x][0]. How does it work for me?
Copy linkTweet thisAlerts:
@miliauthorDec 20.2003 — I'm not able to get to the last step. This time, I'm getting the right value into the selectbox based on the selected id_ccy value, but it is loading only one value.where am i going wrong?

function getStgy()

{

ccySelected = document.forms[0].Id_ccy[document.forms[0].Id_ccy.selectedIndex].text;

var ccy = new Array();

var stgy = new Array();

strategy = new Array();

var count = 0;

for(var x=0;x&lt;arrStgy.length;x++)
{

var temp = arrStgy[x].split("#");

ccy[x] = temp[0]; //value before #

stgy[x] = temp[1]; //value after #

if(ccySelected == ccy[x])

{

//alert("matching done");

var stgyccy = new Array();

stgyccy = stgy[x];

}

}

//alert("BEFORE ["+ccy+"]nAFTER ["+stgy+"]")

fillStgy( form.selectbox, stgyccy);

}
Copy linkTweet thisAlerts:
@Khalid_AliDec 20.2003 — my guess is that u are creating new array each time and pass it to the fill function,if I ma correct you should be getting the last array value filled in the list box
Copy linkTweet thisAlerts:
@miliauthorDec 20.2003 — If I get the last array value filled in the list box, them I'm not filtering my list.I'm populating the entire array.
Copy linkTweet thisAlerts:
@miliauthorDec 20.2003 — Khalid, I'm still stuck with this problem.

Should I build an array again, within my if condition?

Thanks
Copy linkTweet thisAlerts:
@miliauthorDec 20.2003 — Many thanks for your help, khalid.

I resolved the issue

for(var x=0;x&lt;arrStgy.length;x++)

{

var temp = arrStgy[x].split("#");

ccy[x] = temp[0];

if(ccySelected == ccy[x])

{

var temp = arrStgy[x].split("#");

stgy[x] = temp[1];

stgy.sort();

}

}
×

Success!

Help @mili spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...