tags:

views:

210

answers:

2

Hello,

Now i have a select box.

<select multiple size="5" name="interest">
 <optgroup label="Sports">
  <option value="">Footaball</option>
  <option value="">Basketball</option>
  <option value="">Volleyball</option>
 </optgroup>
 <optgroup label="Music">
  <option value="">Folk Musique</option>
  <option value="">Pop Musique</option>
  <option value="">Rock Musique</option>
 </optgroup>
 <optgroup label="Pets">
  <option value="">Dogs</option>
  <option value="">Cats</option>
  <option value="">Rabbits</option>
 </optgroup>
 <option value="other">other</option>
</select>

I want to click "other", and then displays a text field

<input type="text" name="other_interest" />

How can i make it?

Thanks very much.

+3  A: 

You can do this with jQuery. E.g:

$('select[name=interest] option[value=other]').click(function() {
    $('input[name=other_interest]').show();   
})

HTML:

<select multiple size="5" name="interest">
    <!-- ... -->
    <option value="other">other</option>
</select>
<input type="text" name="other_interest" style="display:none" />

If you give an ID to each element you want to operate on, it is even easier to write.

Felix Kling
@Felix, Thanks, it works. And for completing this, i want it to disappear after i click back to the other items.
garcon1986
@garcon1986 You can hide the element with `$('input[name=other_interest]').hide();` Have a look at the documentation: http://api.jquery.com/
Felix Kling
+1  A: 

Quick and dirty (but that's the basic idea):

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
<html>
<head><title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css"><!--
#other{
    display: none;
}
--></style>
<script type="text/javascript"><!--
function checkOther(select){
    if( select[select.selectedIndex].value=="other" ){
        document.getElementById("other").style.display = "inline";
    }else{
        document.getElementById("other").style.display = "none";
    }
}
//--></script>
</head>
<body>

<select multiple size="5" name="interest" onchange="checkOther(this)">
 <optgroup label="Sports">
  <option value="">Footaball</option>
  <option value="">Basketball</option>
  <option value="">Volleyball</option>
 </optgroup>
 <optgroup label="Music">
  <option value="">Folk Musique</option>
  <option value="">Pop Musique</option>
  <option value="">Rock Musique</option>
 </optgroup>
 <optgroup label="Pets">
  <option value="">Dogs</option>
  <option value="">Cats</option>
  <option value="">Rabbits</option>
 </optgroup>
 <option value="other">other</option>
</select>
<input id="other"type="text" name="other_interest" />


</body>
</html>
Álvaro G. Vicario
@thanks Alvaro, it's working perfectly.
garcon1986