1

The below code is working when i paste it in the browser directly (chrome console). But it is not working from my source file

<script type="text/javascript" >
        $(".test").click(function(){
            $(this).parent().find("div").toggle();
        });
    </script>
1
  • Thanks all.. It s working with document.ready :):) Commented May 21, 2012 at 6:07

4 Answers 4

4

Try running it only after the DOM is ready:

$(function(){

  $(".test").on("click", function(){
    $(this).parent().find("div").toggle();
  });

});
Sign up to request clarification or add additional context in comments.

Comments

0

try replace $ by writing jQuery..

like,

jQuery(function(){

Comments

0

make sure you wrap in the document.ready function. it ensure that it will bind the function when the page load completes. Document.ready()

$(document).ready(function(){

  $(".test").on("click", function(){
    $(this).parent().find("div").toggle();
  });

});

Comments

0

Always do this while using jquery

$('document').ready(function(){
     $(function(){
        $(".test").on("click", function(){
        $(this).parent().find("div").toggle();
        });
     });
});

Always put code in the function this will make it work with and with out document ready.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.