سؤال

I was trying to add function2 in my jquery hide method.I can do it through the code which is commented.So,can anyone please rectify my code.I want to call function2 from callback parameter of hide method.

<script>
//        $(document).ready(function () {
//            $("#p1").click(function () {
//                $("#p2").hide("slow", function () {
//                    alert("para2 is now hidden");
//                });
//            });
//        });

$(document).ready(function () {
    $("#p1").click(function () {
        $("#p2").hide("slow", function(){
            function2(); 
        });
    });

function function2() {
    alert("para2 is now hidden");
}
</script>
هل كانت مفيدة؟

المحلول

Problem is, you have a syntax error because you haven't closed your $(document).ready() function or method:

$(document).ready(function () {
    $("#p1").click(function () {
        $("#p2").hide("slow", function(){
            function2(); 
        });
    });
});

JSFiddle

As already mentioned, you can just pass the function's reference if that is all that should be in the callback:

$("#p2").hide("slow", function2);

نصائح أخرى

The problem with your code is, you forgot to close the ready event handler,

   $(document).ready(function () {
     $("#p1").click(function () {
        $("#p2").hide("slow", function(){
            function2(); 
        });
    });
   });

And you can also try this code to improvise your code much better,

   $("#p1").click(function () {
     $("#p2").hide("slow", function2);
   });

if I understand, just pass the function that you want

 $("#p1").click(function () {
            $("#p2").hide("slow", function2);
    });
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top