[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How can I toggle textbox/label visibility without postback?

Posted on 2011-10-21
3
Medium Priority
?
318 Views
Last Modified: 2012-05-12
I am trying to toggle the visibility of a textbox from a value in dropdown. If the value is something other than "None", I would like textbox and it' slabel (Building Name and txtBuilding_Name) to be visible. How can I best do that using jquery or javascript so I don't have to call a postback?

Please see the attached code attempts.
<tr valign="top">
                <td class="tdLeft">
                    Access Timeframe:
                </td>
                <td class="tdRight">
                    <asp:DropDownList ID="ddlAccessTimeframe" runat="server" CssClass="textEntry">
                    </asp:DropDownList>
                </td>
            </tr>
            <tr valign="top">
                <td class="tdLeft">
                    Building Name:
                </td>
                <td class="tdRight">
                    <asp:TextBox ID="txtBuilding_Name" runat="server" MaxLength="30" CssClass="textEntry" />
                    <asp:CustomValidator ID="CustomValidator1" SetFocusOnError="true" runat="server"
                        ClientValidationFunction="AccessTimeframe_RequiredCheck" ErrorMessage="Required"
                        ValidateEmptyText="true" CssClass="failureNotification" ControlToValidate="txtBuilding_Name"></asp:CustomValidator><br />
                </td>
            </tr>

Open in new window

function AccessTimeframe_RequiredCheck(sender, args) {

            if ($('select[id$=ddlAccessTimeframe] :selected').text() == 'None') {
                if (args.Value.length == 0) {
                    args.IsValid = false;
                    return;
                }
            }
            args.IsValid = true;
        }

Open in new window

if ($("#<%=txtBuilding_Name.ClientID%>").val().length <= 0)
                $("#divHideBuildingName").hide('fast');


            $("#<%=ddlAccessTimeframe.ClientID %>").change(function () {
                var selectText;
                selectText = $('select[id$=ddlAccessTimeframe] :selected').text();

                if (selectText == 'None') {
                    $("#divHideBuildingName").show('fast');
                }
                else
                    $("#divHideBuildingName").hide('fast');
            });

Open in new window

0
Comment
Question by:devnewbee
  • 2
3 Comments
 
LVL 83

Expert Comment

by:leakim971
ID: 37009369
What about :
<script>
$(document).ready(function() {

            $("#<%= ddlAccessTimeframe.ClientID %>").change(function () {
                var selectText = $(this).text();

                if (selectText == 'None') { // double check the case
                    $("#<%= divHideBuildingName.ClientID %>").closest("tr").show();
                }
                else {
                    $("#<%= divHideBuildingName.ClientID %>").closest("tr").hide();
                }
            });

})
</script>

Open in new window

0
 
LVL 83

Accepted Solution

by:
leakim971 earned 2000 total points
ID: 37009416
Or :
<script>
$(document).ready(function() {

            $("#<%= ddlAccessTimeframe.ClientID %>").change(function () {
                var selectText = $(this).val();

                if (selectText == 'None') { // double check the case
                    $("#<%= divHideBuildingName.ClientID %>").closest("tr").show();
                }
                else {
                    $("#<%= divHideBuildingName.ClientID %>").closest("tr").hide();
                }
            });

})
</script>
0
 

Author Closing Comment

by:devnewbee
ID: 37028182
Thank you.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
The viewer will learn how to count occurrences of each item in an array.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Suggested Courses
Course of the Month20 days, 2 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question