Jump to content

[SOLVED] javascript is not working when Loading/refresh the form


raj86

Recommended Posts

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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)">

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.