DKR DKR - 14 days ago 9
jQuery Question

jQuery function not getting called from C# code behind with RegisterStartupScript

I have a

jQuery
function:

<script src="Scripts/jquery-3.1.1.min.js"></script>
<script type="text/javascript">

$(document).ready(function () {
alert("1");
function hideInfo() {
alert("2");
$("#h3memberInfo").fadeOut("slow");
};
});

</script>


I am trying to call above function from c# as below.

ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "alert", "hideInfo();", true);


On page load I get
alert("1")
but
alert("2")
is never triggered.

What wrong am I doing?

Answer

As your function hideInfo() is wrapped inside the $(document).ready block, its not available in the global scope as you try from the code behind.

You can modify your code as below,

<script src="Scripts/jquery-3.1.1.min.js"></script>
    <script type="text/javascript">

        function hideInfo() {    
           $(document).ready(function () {
                alert("1");
                $("#h3memberInfo").fadeOut("slow");                
           });
        }

    </script>