tags:

views:

28

answers:

2

I'd like to show / hide a div within a td tag based on the select value chosen. I cannot get the label text to appear, only the textbox. Can someone please give me a hand with this?

Here is my code so far

<script>
function toggleOther(chosen){
if (chosen == 'oth') {
  document.myform.other.style.visibility = 'visible';
} else {
  document.myform.other.style.visibility = 'hidden';
  document.myform.other.value = '';
}
}
</script>
<form name="myform">
  <tr>
    <td>
      <select name="values" size="1" onchange="toggleOther( document.myform.values.options[ document.myform.values.selectedIndex ].value );">
      <option value=" " selected="selected"> </option>
      <option value="name">Name</option>
      <option value="age">Age</option>
      <option value="oth">Other</option>
      </select>
    </td>
    <td>
      <div style="visibility:hidden">
        <label>adfdasfgsfg</label>
        <input type="text" name="other" value="" size="25" />
      </div>
    </td>
  </tr>
</form>
A: 

The problem is that you have set visibility on your containing <div> to hidden but your javascript is only showing your input. (document.myform.other.style.visibility)

To make it work, simply give your <div> an id and show and hide that instead:

// ... snip ...
function toggleOther(chosen){
var piece_to_hide = document.getElementById("otherValues");
if (chosen == 'oth') {
  piece_to_hide.style.display = 'block';
} else {
  piece_to_hide.style.display = 'none';
  document.myform.other.value = '';
}
}
// ... snip ...
  <div id="otherValues" style="display:none">
     <label>adfdasfgsfg</label>
     <input type="text" name="other" value="" size="25" />
  </div>
Sean Vieira
Hi Sean and thanks for the help. I'm not at all javascript savvy. I've tried your altered code but it still won't work.
jim
*Smacks head* @jim -- apologies, I assumed the page was already loaded -- I've moved the `document.getElementById` call inside the function; it should work now.
Sean Vieira
Thanks Sean, I'll try it. brb.
jim
Thanks so much Sean. That did work.
jim
+1  A: 

Give the element an id:

<div id="Other" style="visibility:hidden">

Now you can easily access it in the code:

document.getElementById('Other').style.visibility = 'visible';

Edit:

Here's the code with the changes, tested and working:

<script>
function toggleOther(chosen){
if (chosen == 'oth') {
  document.getElementById('Other').style.visibility = 'visible';
} else {
  document.getElementById('Other').style.visibility = 'hidden';
  document.myform.other.value = '';
}
}
</script>
<form name="myform">
  <tr>
    <td>
      <select name="values" size="1" onchange="toggleOther( document.myform.values.options[ 

document.myform.values.selectedIndex ].value );">
      <option value=" " selected="selected"> </option>
      <option value="name">Name</option>
      <option value="age">Age</option>
      <option value="oth">Other</option>
      </select>
    </td>
    <td>
      <div id="Other" style="visibility:hidden">
        <label>adfdasfgsfg</label>
        <input type="text" name="other" value="" size="25" />
      </div>
    </td>
  </tr>
</form>
Guffa
Thanks Guffa. I was able to hide and show the textbox using your code. The only thing is that after the div is shown, the text does not disappear with the textbox. What I need is for both the textbox as well as the text to show and hide.
jim
@jim: It works just fine when I try it. I added the complete edited code above.
Guffa