jQuery's $() function always returns 'undefined' with AJAX

3.2k views Asked by At

i've noticed that popup shows BEFORE text gets updated in the textbox, i guess js gets called before the page gets rendered ... that would explain the 'undefined' popup ... how do i make sure js gets called AFTER the page is rendered?

rewriting to make it as simple as possible:

<body>
<form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:TextBox ID="txtRcaNotes" runat="server" TextMode="MultiLine" Width="800px"></asp:TextBox><br />
            <asp:Button ID="btnDoneWithRcs" runat="server" OnClick="btnDoneWithRcs_Click" Text="Action Completed / Update Notes" />
        </ContentTemplate>
    </asp:UpdatePanel>
</form>

<script type="text/javascript">

        var prm = Sys.WebForms.PageRequestManager.getInstance();
            prm.add_endRequest(
            function(){doStuff();}
            );

        function doStuff()
        {
            $(document).ready(function() {
                                $('txtRcaNotes').hide(); 
                                alert($('txtRcaNotes').attr('id'));
                                });
        }

</script>
</body>

Code Behind:

protected void btnDoneWithRcs_Click(object sender, EventArgs e)
{
    txtRcaNotes.Text += "asdfadf";
}

TEXTBOX DOESN'T GET HIDDEN, ALERT() RETURNS 'UNDEFINED'

alt text

2

There are 2 answers

0
Crescent Fresh On BEST ANSWER

You're just missing your id selector syntax. Try:

$('#<%= txtRcaNotes.ClientID %>').hide(); 
alert($('#<%= txtRcaNotes.ClientID %>').attr('id'));

Note the addition "#" prepended before each selector.

1
Jason Miesionczek On

One thing you could try is using Firebug, or some other DOM inspector and check the actual element IDs that are being generated by ASP.NET before and after your AJAX call and see if they are the same.