1
votes

I want to set visibility of a text box depending on the value of a combo box's selected value. This is my code:

<table width="500" align="center">
  <tr>
    <td><span> Name:</span></td>
    <td><input name="name"  type="text" id="name" size="40" class=""/></td>
  </tr>
  <tr>
    <td><span>Email ID:</span></td>
    <td><input name="email"  type="email" id="email" size="40" class=""/></td>
  </tr>
  <tr>
    <td><span>Select:</span></td>
    <td>
      <select id="edition">
        <option value="none" selected>----Select ----</option>
        <option id="1">A</option>
        <option id="2">B</option>
      </select>
    </td>
  </tr>
  <tr>
    <td><span>Number of Licenses</span></td>
    <td><input type="text" id="licenseNo" size="5" value="30" /></td>
  </tr>
</table>

I want the licenseNo text box visible if the value of combo box is B. I don't know how to do this.

2
are you ok to do via javascript?DevelopmentIsMyPassion
yes. its better to use javascriptApb
I updated answer. Just added tr and given id to it. Now you can hide entire row.DevelopmentIsMyPassion

2 Answers

0
votes

You can also use it like below. Just use value to get selected text

<table width="500" align="center" >
  <tr>
    <td><span> Name:</span></td>
    <td><input name="name"  type="text" id="name" size="40" class=""/></td>
  </tr>
  <tr>
    <td><span>Email ID:</span></td>
    <td><input name="email"  type="email" id="email" size="40" class=""/></td>
  </tr>
  <tr>
    <td><span>Select:</span></td>
    <td>
     <select id="edition" onchange="func()">
      <option value="none" selected >----Select ----</option>
      <option id="1">A</option>
      <option id="2">B</option>
     </select>
    </td>
  </tr>
  <tr id ="trhide">
    <td><span>Number of Licenses</span></td>
    <td><input type="text" id="licenseNo" size="5" value="30" /></td>
  </tr>
</table>

<script type="text/javascript">
 function func() {
   var elem = document.getElementById("edition");

   if(elem.value == "B") {
      document.getElementById("trhide").style.visibility = "visible"; 
   } else {
     document.getElementById("trhide").style.visibility = "hidden"; 
   }
 }
</script>

see fiddle here

1
votes

Add event listener using JS:

<script type="text/javascript">
function func() {
    var elem = document.getElementById("edition");
    if(elem.options[elem.selectedIndex].innerHTML == "B") {
        document.getElementById("licenseNo").style.display = "";
    } else {
        document.getElementById("licenseNo").style.display = "none";
    }
}
</script>

<select id="edition" onchange="javascript:func();">
    ...
</select>

also don't forget to call func() at the start, when your page is just loaded.