I think the problem was i was writing .innerHtml and overwriting the form elements, is that correct, sorry to waste your time
Hi,
I am trying to toggle the visibility of a div containing some form elements. The idea is that if the person selects a particular value of a select box an onchange event handler will make a particular div containing those form elements disappear.When they choose any other option the div will reappear.
if(chosen_option != option_that_makes_id dissappear){ document.getElementById(id).style.visibility = 'hidden'; document.getElementById(id).innerHTML = 'the if statement is working'; }else if(chosen_option == option_that_makes_id dissappear){ document.getElementById(id).style.visibility = 'visible';
}
The problem i am having is the the div will disappear properly but then when I select another value from the check list the div reappears but no longer contains the form values.
I put the .innerHTML in the if statement and then when that condition is met i just see the text the if statement is working but not the form elements Is this a feature of JavaScript or is am I doing something wrong?
Here is the full unedited javascript function
function change_div_visibility(id,select_id,div){//id is the id of the select box and the name of the div which you want to change the visibility of .
var select_option = document.getElementById(select_id).options;
var chosen_option = select_option[select_option.selectedIndex].value;
if(chosen_option != id){
document.getElementById(div).style.display = 'block';
document.getElementById(div).innerHTML = 'the if statement is working';
}else if(chosen_option == id){
document.getElementById(div).style.display = 'none';
document.getElementById(div).innerHTML = 'the else statement is working but for some reason the visibility of the div is not changing. ';
}
}
and the section of the form which i am trying to manipulate
<p><label for='buying_options'>Select Buying Option</label><select onchange=\"change_div_visibility('new_buying_1','buying_options1','div1')\" id='buying_options1' name='buying_options1'><option value='undefined'>Select Buying Option</option>
<option value='new_buying_1'>New Buying Option</option>";
$y = '';
foreach($buying_options_arr as $key=>$value){
$x.= "<option value='$key' >$value</option>";
$y.= '<option value='.$key.'>'.$value.'</option>';
}
$x.= "</select> or</p> ";
$x.= "<div id='div1' >
<p><label for='add_new_buying_option'>Add new Buying Option: </label><input id='add_new_buying_option' type='text' name='add_new_buying_option1' /></p>\n";
$x.= "<p><label for='compression' >New Unit Quantity: </label><input type='text' id='compression' name='unit_quantity1' /></p>\n
";
Thanks Andrew