raj86 Posted October 27, 2009 Share Posted October 27, 2009 Hello friends: i am having javascript , that is working but the problem here is its not working when we are loading the page first time or when we refresh the form.................it is working as soon as we select any of employee type...please go through the code and help me........i think i am not using onchange event correctly in employee type <script language="javascript"> function addOption(selectList,optionText,optionValue) { var newOption = document.createElement('OPTION'); newOption.text = optionText; newOption.value = optionValue; selectList.options.add(newOption); } function changeList(list1,list2) { list2.length = 0; if(list1.options[list1.selectedIndex].value == 2) { addOption(list2,"ACTD","ACTD"); addOption(list2,"ALEC","ALEC"); addOption(list2,"ASOP","ASOP"); addOption(list2,"ASTP","ASTP"); addOption(list2,"DIRO","DIRO"); addOption(list2,"DLIB","DLIB");addOption(list2,"LECT","LECT"); addOption(list2,"PFAT","PFAT"); addOption(list2,"PROF","PROF");addOption(list2,"TA","TA"); addOption(list2,"VFAC","VFAC"); addOption(list2,"VPRF","VPRF"); } else if(list1.options[list1.selectedIndex].value == 3) { addOption(list2,"ACCO","ACCO"); addOption(list2,"APRG","APRG"); addOption(list2,"ASTR","ASTR"); addOption(list2,"COCH","COCH"); addOption(list2,"CSHR","CSHR"); addOption(list2,"CTOR","CTOR"); addOption(list2,"DRVR","DRVR"); addOption(list2,"DUDT","DUDT"); addOption(list2,"ELER","ELER"); addOption(list2,"FINO","FINO"); addOption(list2,"HSPT","HSPT"); addOption(list2,"JAAT","JAAT"); addOption(list2,"JOAT","JOAT"); addOption(list2,"JTEC","JTEC"); addOption(list2,"LABT","LABT"); addOption(list2,"LBST","LBST"); addOption(list2,"LIST","LIST");addOption(list2,"MEDA","MEDA"); addOption(list2,"MO","MO"); addOption(list2,"OFST","OFST"); addOption(list2,"PA","PA");addOption(list2,"PAST","PAST");addOption(list2,"PRGR","PRGR"); addOption(list2,"SCOP","SCOP"); addOption(list2,"SLAT","SLAT"); addOption(list2,"SMDA","SMDA"); addOption(list2,"SMDO","SMDO"); addOption(list2,"SNRS","SNRS"); addOption(list2,"STCH","STCH");addOption(list2,"SUPV","SUPV"); addOption(list2,"WSPT","WSPT"); } else if(list1.options[list1.selectedIndex].value == 4) { addOption(list2,"ATDT","ATDT");addOption(list2,"CHWK","CHWK"); addOption(list2,"LIDT","LIDT"); addOption(list2,"NRSE","NRSE"); addOption(list2,"OAST","OAST"); } } </script> <br><br> <label>3. Employee Type</label> <font color="##FF0000" >*</font> <select name="emptyp" onChange="changeList(document.forms.form2.emptyp,document.forms.form2.desig)"> <option value="2" <?php if (isset($_POST['emptyp']) && $_POST['emptyp'] == '2') { echo "selected='selected'"; }?> >2 (Teaching faculty)</option> <option value="3" <?php if (isset($_POST['emptyp']) && $_POST['emptyp'] == '3') { echo "selected='selected'"; }?> >3 (Supporting staff)</option> <option value="4" <?php if (isset($_POST['emptyp']) && $_POST['emptyp'] == '4') { echo "selected='selected'"; }?> >4 (Helping staff)</option> </select> <br> <br> <label >9. Designation</label> <select name="desig"> </select> <br><br> Quote Link to comment Share on other sites More sharing options...
seanlim Posted October 27, 2009 Share Posted October 27, 2009 You have to call the function changeList(document.forms.form2.emptyp,document.forms.form2.desig) once when the page is loaded, either by using the onload event or within <script> tags after the <form>. According to your code, the changeList function is only called when there is a change in the select box. Therefore, the options will NOT be added to the second select box unless there is a change in the first select box. Quote Link to comment Share on other sites More sharing options...
raj86 Posted October 28, 2009 Author Share Posted October 28, 2009 hello seanlim thanks for replying..... i am new in programming........can you suggest me how to use onLoad event in this case Quote Link to comment Share on other sites More sharing options...
seanlim Posted October 28, 2009 Share Posted October 28, 2009 <body onload="changeList(document.forms.form2.emptyp,document.forms.form2.desig)"> Quote Link to comment Share on other sites More sharing options...
raj86 Posted October 28, 2009 Author Share Posted October 28, 2009 I allready tried this one ....this thing is not working for me...... Quote Link to comment Share on other sites More sharing options...
seanlim Posted October 28, 2009 Share Posted October 28, 2009 Does your script work if you change the value of the SELECT? Will the other SELECT get populated with options? Quote Link to comment Share on other sites More sharing options...
raj86 Posted October 28, 2009 Author Share Posted October 28, 2009 yes when i am using onChange event and if i select any employee type it is working ...values are changing ............but if i replace it with onLoad nothing is happing, even the values are not coming Quote Link to comment Share on other sites More sharing options...
seanlim Posted October 28, 2009 Share Posted October 28, 2009 You should not REPLACE it with the onload event. Both the onload event in the BODY tag and the onchange event in the SELECT tag should be present and calling the same function with the same paramenters. <body onload="changeList(document.forms.form2.emptyp,document.forms.form2.desig)"> ... <select name="emptyp" onChange="changeList(document.forms.form2.emptyp,document.forms.form2.desig)"> Quote Link to comment Share on other sites More sharing options...
raj86 Posted October 28, 2009 Author Share Posted October 28, 2009 thank you seanlim ...its working Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.