Search code examples
javascriptjqueryasp.nettextboxtextinput

Hidden ASP.NET textbox values not working jquery


I have an ASPX page where in I need to set the value of "FirstName" with "YourName". Both are textboxes. "YourName" is set with visible=false on the page. Problem is Jquery works only visible=true.

My goal is to set Firstname with Yourname with the condition that YourName has visible=false.

Here are the details

<asp:TextBox runat="server" ID="fnameTextBox" />
<asp:TextBox runat="server" ID="ynameTextBox" visible="false" />

<script type="text/javascript">
    $(document).ready(function () {
        $('input:text[id*=fnameTextBox]').val($('input:text[id*=ynameTextBox]').val());
    });
</script>

I can store the value in 'ynametextbox' to some other type or variable but I don't know how to reference in Jquery (if possible).


Solution

  • Here is the example with the HiddenField as requested:

    Javascript:

    <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script>
        $(document).ready(function () {
            $('#<%=fnameTextBox.ClientID %>').val($('#<%=hiddenTextYourName.ClientID %>').val());
        });
    </script>
    

    HTML

    <asp:TextBox runat="server" ID="fnameTextBox" />
    <asp:HiddenField runat="server" ID="hiddenTextYourName" Value="Hanlet" />
    

    Hidden field is not visible to the user of course, but beware because they can still see the value in the HTML.

    Good luck..