1

I have a simple page with three check box with the auto populate the data by jQuery and the fourth one has no data(empty).

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="MasterPageWithHTMLInput.aspx.cs" Inherits="Test_WSMS_TV.MasterPageWithHTMLInput" %>

    <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>

        <script type="text/javascript">
            // alert('akash');
            $(document).ready(function() {
                var arr = ["val1", "val2", "val3"];
                $('#check label').each(function(index) {
                    if (index < arr.length)
                        $(this).text(arr[index]);    
                });    
            });
        </script>
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

        <div class="container" id="check">
            <input type="checkbox" id="CheckBox3" runat="server" />
            <label>
            </label>
            <input type="checkbox" id="CheckBox4" runat="server" />
            <label>
            </label>
            <input type="checkbox" id="CheckBox5" runat="server" />
            <label>
            </label>
            <input type="checkbox" id="CheckBox6" runat="server" />
            <label>
            </label>
        </div>

    </asp:Content>

I want to hide the Check box and label which has no data (fourth one) by jQuery.

<input type="checkbox" id="CheckBox6" runat="server"/>
        <label>
        </label>

how can i do it by jQuery?

1
  • i want to make dynamic like var arr = ["val1", "val2"] then hide the former two check box with label. Commented Aug 12, 2015 at 9:01

3 Answers 3

2

Perhaps try one of the following

$("#CheckBox6").hide();

or

$("#CheckBox6").toggle();
Sign up to request clarification or add additional context in comments.

Comments

1

You can use :empty selector

Select all elements that have no children (including text nodes).

Script

$('#check label:empty').hide();
$('#check label:empty').prev(':checkbox').hide();

Comments

0

I have found an dynamic solution. populate ID and hide the rest of the empty text box

<script type="text/javascript">
       // alert('akash');
        $(document).ready(function () {
            var arr = ["val1", "val2", "val3"];
            $('#check label').each(function (index) {
                if (index < arr.length)
                    $(this).text(arr[index]);
                else {
                    //                    $('#check label:empty').prev(':checkbox').hide();
                    var i = index + 3;
                    var id = "#CheckBox" + i;
                    alert(id);
                    $(id).hide();
                }
            });
         //   $("#CheckBox6").hide();
        });

    </script>

and in main place holder clientidmode="Static" in asp.net is required.

<input type="checkbox" id="CheckBox6" runat="server" clientidmode="Static" />
        <label>
        </label>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.