java script

Tamer_Ahmed

Centurion
Joined
Dec 20, 2003
Messages
159
Location
Egypt
hi all i'm new with java script but i'm doing great with .net
i have page contain checkbox called chk
and textbox called First and textbox called second when the user check the checkbox
the textboxes.visible should be = true actually i can do it with .net but i don't want the page to postback so i want to write using javascript does anyone have any idea about how to write this code
 
This should do it for you.

<script type="text/javascript">
function setVisibility(checked){
var textbox1;
var textbox2;
if (document.getElementById){
textbox1 = document.getElementById('first');
textbox2 = document.getElementById('second');
} else {
textbox1 = document.all('"first');
textbox2 = document.all('second');
}

if (checked){
textbox1.style.visibility='visible';
textbox2.style.visibility='visible';
} else {
textbox1.style.visibility='hidden';
textbox2.style.visibility='hidden';
}
}
</script>


<asp:CheckBox ID="chk" Runat="server" Checked="False" onClick="setVisibility(this.checked);"></asp:CheckBox>

<asp:TextBox ID="first" Runat="server" style="visibility:hidden;"></asp:TextBox>

<asp:TextBox ID="second" Runat="server" style="visibility:hidden;"></asp:TextBox>


Notice that I used the style="visibility:hidden;" in the textbox server tags instead of Visibility="False". This was because the textboxes don't get added to the elements collection, and therefore cannot be accessed in the javascript.

Also, this javascript should work with all browsers except, Netscape 4. It was checked with IE 4+, Netscape 6+, Opera, and Firefox.
 
Back
Top