hardik said on 18/04/2006 2:23 AM AEST:
hi friends
i am very new to java script.if anyone can help me out then
i will be thankful.
i am using php and mysql in my learning project and i have
two list boxes,now whenever user presses first listbox i mean user
selects listbox then approprieate records should be come in second
listbox.can i do it with javascript? i.e if user selects list item
'class bca' then all the records related to 'class bca' should come to
second listbox.i wants to store records in 3*3 array in javascript
first then i wants to use it. but i am unsuccessfull so i need your
help friends.
This is not chat. Please use correct punctuation and capitalisation in
your posts. You've taken the time to double-space paragraphs and indent
the first line, so is the chat-style just to be hip?
Anyhow, I presume you mean you have your data in an array object like:
var theData = [
['A1','A2','A3'],
['B1','B2','B3'],
['C1','C2','C3']
];
You don't say what values you intend putting in the first select
element, nor how the values for the second are chosen. I'll presume
that you want to put A1, B1 and C1 as the values of options in your
first select, and the rest of the associated array in the second select
depending on which one is chosen in the first.
For example (lacking feature detection and fallback...):
<title>Select play</title>
<script type="text/javascript">
/* Data matrix
*/
var theData = [
['A1','A2','A3'],
['B1','B2','B3'],
['C1','C2','C3']
];
/* Load the first select with options
*/
function initSel(sel)
{
// Remove current options
sel.options.length = 0;
var val;
// Insert one option for each array in theData
for (var i=0, len=theData.length; i<len; ++i){
// Use the first element in each array as the value & text
val = theData[i][0];
sel.options[i] = new Option(val, val);
}
}
/* selA is a reference to the source select
* selB is a reference to the select to update
*/
function fillSel(selA, selB)
{
// Get the value of the selected option
var val = selA[selA.selectedIndex].value;
var valArray, valB;
// Find which array the value came from (the value
// of the first item in each array must be unique)
for (var i=0, len=theData.length; i<len; ++i){
valArray = theData[i];
// When a match found, use the rest of the array items
// to set the second select's option value & text
if (valArray[0] == val){
selB.options.length = 0;
for (var j=1, m=valArray.length; j<m; ++j){
valB = valArray[j];
selB.options[j-1] = new Option(valB, valB);
}
}
}
}
</script>
</head>
<body onload="initSel(document.formA.sel_1);">
<form name="formA" action="">
Option 1
<select name="sel_1" style="width:13em"
onchange="fillSel(this, this.form.sel_2);">
<option>JavaScript required
</select>
Option 2
<select name="sel_2" style="width:13em">
<option>Select option 1 first
</select>
</form>
</body>
--
Rob
Group FAQ: <URL:http://www.jibbering.com/FAQ>