Sowmya
Sowmya

Reputation: 26989

jquery click function is not working

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>

Upvotes: 1

Views: 258

Answers (4)

Muhammad Raheel
Muhammad Raheel

Reputation: 19882

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.

Upvotes: 0

Ravi Gadag
Ravi Gadag

Reputation: 15881

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();
  });

});

Upvotes: 0

Dolly
Dolly

Reputation: 1052

try replace $ by writing jQuery..

like,

jQuery(function(){

Upvotes: 0

Sampson
Sampson

Reputation: 268492

Try running it only after the DOM is ready:

$(function(){

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

});

Upvotes: 4

Related Questions