Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide a select tag?

I have this simple form:

<form method="post" action="index.php" >
<table>

<tr>
<td>
Sex:
</td>
<td>
  <select name="sex" >
   <option value="e">Select  </option>
   <option value="girl">Girl  </option>
   <option value="boy">Boy  </option>
  </select>
</td>
</tr>

<tr>
<td>
Accessories:
</td>
<td>
  <select name="earrings" >
   <option value="e">Select  </option>
   <option value="gold">gold  </option>
   <option value="silver">silver  </option>
  </select>
</td>
</tr>


</table>

<br>
<input type="submit" size="10" value="Go" name="go">
</form>

and I want that when I click on "boy" in the first select then this block have to dissapears:

<tr>
<td>
Accessories:
</td>
<td>
  <select name="earrings" >
   <option value="e">Select  </option>
   <option value="gold">gold  </option>
   <option value="silver">silver  </option>
  </select>
</td>
</tr>

I can do this with CSS ? If not, I can do this with javascript ?

like image 980
xRobot Avatar asked Dec 12 '10 18:12

xRobot


People also ask

How do you hide a select tag?

The hidden attribute hides the <select> element. You can specify either 'hidden' (without value) or 'hidden="hidden"'. Both are valid. A hidden <select> element is not visible, but it maintains its position on the page.

Which attribute is used to hide selection?

The hidden global attribute is a Boolean attribute indicating that the element is not yet, or is no longer, relevant. For example, it can be used to hide elements of the page that can't be used until the login process has been completed. Browsers won't render elements with the hidden attribute set.

How do you hide something in HTML?

To hide an element, set the style display property to “none”.


1 Answers

You can't do this in CSS, but you can in JavaScript

function hide(){
var earrings = document.getElementById('earringstd');
earrings.style.visibility = 'hidden';
}

function show(){
var earrings = document.getElementById('earringstd');
earrings.style.visibility = 'visible';
}

Just make sure that your td has id=earringstd

Then create function:

function genderSelectHandler(select){
if(select.value == 'girl'){
show();
}else if(select.value == 'boy'){
hide();
}}

Now all you have to is change your gender select tag to:

<select name="sex" onchange="genderSelectHandler(this)">
like image 135
Goran Jovic Avatar answered Oct 05 '22 13:10

Goran Jovic