views:

196

answers:

1

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.= '&lt;option value='.$key.'&gt;'.$value.'&lt;/option&gt;';
   }
        $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

+1  A: 

Have you tried changing style.display to 'none' or 'block' instead?

No Refunds No Returns
+1. You're a bit quicker than me.
David Stratton
This is not quite the same thing. Setting visibility to "hidden" hides the element from view, but it still takes up space. Setting display to "none" is the equivalent of wiping the element out; it no longer takes up any space.
jboxer
Its not wiped out, it remains there, just takes no space.
Salman A
display is far and away the property chosen to implement the requested functionality. Large blocks of empty space are ugly and (gasp!) "confuse the user."
No Refunds No Returns
Salman: Correct, I meant "wiped out" in the sense that it takes up no space. It can still be unhidden of course :)No Refunds No Returns: Agreed, but since this is the property he was changing, I figure he has a reason to use it over display.
jboxer