Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

hide / display select box

Status
Not open for further replies.

nrastogi

Programmer
Jul 19, 2002
52
US
Hello all,

Could you please help me out on this...

I would like to hide and display a select box depending on the value selected in another select box.

Thanks so much in advance for assistance.

NR
 
Code:
<head>
  <script language=&quot;javascript&quot;>
    function showHide(selList){
      if(selList.value == &quot;show&quot;){
        document.myForm.list2.style.display = &quot;block&quot;;
      }
      else{
        document.myForm.list2.style.display = &quot;none&quot;;
      }
    }
  </script>
</head>
<body>
  <form name=&quot;myForm&quot;>
    <select name=&quot;list1&quot; onchange=&quot;javascript:showHide(this)&quot;>
      <option value=&quot;show&quot;>Show</option>
      <option value=&quot;hide&quot;>Hide</option>
    </select>
    <br>
    <select name=&quot;list2&quot;>
      <option value=1>Option 1</option>
      <option value=2>Option 2</option>
      <option value=3>Option 3</option>
    </select>
  </form>
</body>
 
NR,

This code will do the job:

Code:
<HTML>
<HEAD>
<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
<!--

	function showHideOtherList()
	{
		var formObj = document.forms['myForm'];
		formObj.otherList.style.display = formObj.showHideList[formObj.showHideList.selectedIndex].value;
	}

//-->
</SCRIPT>
</HEAD>
<BODY>

<FORM NAME=&quot;myForm&quot;>
<SELECT NAME=&quot;showHideList&quot; onChange=&quot;showHideOtherList();&quot;>
	<OPTION VALUE=&quot;block&quot;>Show other list</OPTION>
	<OPTION VALUE=&quot;none&quot;>Hide other list</OPTION>
</SELECT>

<BR><BR>
<SELECT NAME=&quot;otherList&quot;>
	<OPTION>Option 1</OPTION>
	<OPTION>Option 2</OPTION>
	<OPTION>Option 3</OPTION>
</SELECT>

<BR><BR>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Morbi nec lectus. Ut condimentum. Maecenas non wisi et justo adipiscing porttitor.

</FORM>

</BODY>
</HTML>

Hope this helps!

Dan
 
Thanks so much guys... I tried LV's code and it worked like a charm..

I have a label and the select box next to the label. When I run the code, it moves the select box show up just below the label, instead next to the label.

Any ideas??

Also, Is there a way to hide & display the label as well.

Thanks so much,
NR




 
NR,

This will also show/hide the label:

Code:
<HTML>
<HEAD>
<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
<!--

	function showHideOtherList()
	{
		var formObj = document.forms['myForm'];
		document.getElementById('showHideDIV').style.display = formObj.showHideList[formObj.showHideList.selectedIndex].value;
	}

//-->
</SCRIPT>
</HEAD>
<BODY>

<FORM NAME=&quot;myForm&quot;>
List Box 1: <SELECT NAME=&quot;showHideList&quot; onChange=&quot;showHideOtherList();&quot;>
	<OPTION VALUE=&quot;block&quot;>Show other list</OPTION>
	<OPTION VALUE=&quot;none&quot;>Hide other list</OPTION>
</SELECT>

<BR><BR>
<DIV ID=&quot;showHideDIV&quot;>
	List Box 2: <SELECT NAME=&quot;otherList&quot;>
		<OPTION>Option 1</OPTION>
		<OPTION>Option 2</OPTION>
		<OPTION>Option 3</OPTION>
	</SELECT>
</DIV>

<BR><BR>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Morbi nec lectus. Ut condimentum. Maecenas non wisi et justo adipiscing porttitor.

</FORM>

</BODY>
</HTML>

Hope this helps!

Dan



Hope this helps!

Dan
 
Hi LV,

Here is the code...

I would like to display enduser select only if the value of report type is 2.

function dispenduser() {

if (document.chsreport.reportty.value == 2) {

document.chsreport.enduser.style.display = &quot;block&quot;;

}
else {

document.chsreport.enduser.style.display = &quot;none&quot;;

}
}




<tr>
<td span class=&quot;bold_bodytext&quot;>Type of Report</td>
<td>
<select name=&quot;reportty&quot; size=&quot;1&quot; onChange=&quot;dispenduser();&quot;>

<option value=&quot;0&quot;>Select a Report </option>
<option value=&quot;1&quot;>Reseller Sales by Vendor </option>
<option value=&quot;2&quot;>Reseller Sales by End User</option>
<option value=&quot;3&quot;>Vendor Sales by Product </option>
</select>
</td>
</tr>



<tr>
<td align=&quot;left&quot; bgcolor=&quot;#C0C0C0&quot; span class=&quot;bold_bodytext&quot;> End User
<select name=&quot;enduser&quot; size=&quot;1&quot; >
<option value=&quot;0&quot;>Select this option </option>
<option value=&quot;abc&quot;> ABC </option>
<option value=&quot;xyz&quot;> XYZ </option>
</select>
</td>
</tr>

 
Hi Billy,

I ran your code. Ideally, This is exactly what I want. But, I can not use this technique as I can not hard code the values of the select to block or none. I want something happen conditionally.

Please let me know if you could provide me the solution by looking at my code.

Thanks so much again.

BTW: I figured out why the select was displaying in the next row. There was a </TD> & <TD> tag missing for label and enduser select box.

Now, the only issue is how to hide the label if I use LV's solution or yours &quot;modified&quot; soultion..
NR
 
NR,

Try this code - a modified version of yours:

Code:
<HTML>
<HEAD>
<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
<!--

	function dispenduser()
	{
		var formObj = document.forms['chsreport'];
		if (formObj.reportty[formObj.reportty.selectedIndex].value == 2)
			document.getElementById('showHideRow').style.display = 'block';
		else
			document.getElementById('showHideRow').style.display = 'none';
	}

//-->
</SCRIPT>
</HEAD>

<BODY>
<FORM NAME=&quot;chsreport&quot;>

<table>
<tr>
   <td class=&quot;bold_bodytext&quot;>Type of Report</td>
   <td>   
      <select name=&quot;reportty&quot; size=&quot;1&quot; onChange=&quot;dispenduser();&quot;>
         <option value=&quot;0&quot;>Select a Report</option>    
         <option value=&quot;1&quot;>Reseller Sales by Vendor</option>
         <option value=&quot;2&quot;>Reseller Sales by End User</option>
         <option value=&quot;3&quot;>Vendor Sales by Product</option>
      </select>
   </td>
</tr>

<tr id=&quot;showHideRow&quot;>
   <td align=&quot;left&quot; bgcolor=&quot;#C0C0C0&quot; class=&quot;bold_bodytext&quot;>End User</td>
   <td>
      <select name=&quot;enduser&quot; size=&quot;1&quot; >
         <option value=&quot;0&quot;>Select this option</option>
         <option value=&quot;abc&quot;>ABC</option>
         <option value=&quot;xyz&quot;>XYZ</option>
      </select>
   </td>
</tr>
</table>

</FORM>
</BODY>
</HTML>

Hope this helps!

Dan



Hope this helps!

Dan
 

Sorry - I forgot to mention...

Basically, the code hides or shows the whole second row.

Dan



Hope this helps!

Dan
 
Hi Dan,

Thanks so very much for all help. This works quite well.

Thanks again a lot.

Regards, NR
 
Hi Dan,

Just a final question. Is this code compatible on old browsers like IE5 and up. Netscape?

Thanks much,
NR
 

I'm not sure as to how far back this will be compatible. I can only suggest you try it and find out, as I don't have access to all browsers.

Dan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top