Saturday 15 October 2011

How do grab the value from label control into jQuery?




jQuery:

$(document).ready(function () {

        $("#btnUpload").click(function () {


            $("#Notes", top.document).val('test');

        });

    });



ASPX code:

<asp:Label ID="lblNames" runat="server" visible="true" ></asp:Label>




Get the value of lblNames using val() function and pass it.

Example:
$(document).ready(function () {

        $("#btnUpload").click(function () {

           var lblNamesValue = $("#lblNames").val();

            $("#Notes", top.document).val(lblNamesValue);

        });

    });

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {

            $('#<%= btnTest.ClientID %>').click(function () {
                var labelText = $('#<%= lblNames.ClientID %>').html();
                alert(labelText);//Do you what you want with labeltext I am jsut displaying alert
                return false;
            });

        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <asp:Label ID="lblNames" runat="server" Visible="true" Text="This is test"></asp:Label><br />
    <asp:Button ID="btnTest" runat="server" Text="Button" />
    </form>
</body>
</html>

or another way is like this, in which find all the span which contains the filename and show their value:
<script type="text/javascript">

    $(document).ready(function() {

        $("#btnUpload").click(function() {


            var allFiles = 'You Selected:';
            $('.MultiFile-title').each(function() {
                allFiles = allFiles + '\n' + $(this).html();

            });
            alert(allFiles);

        });

    });
</script>