Reputation: 26989
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
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
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
Reputation: 268492
Try running it only after the DOM is ready:
$(function(){
$(".test").on("click", function(){
$(this).parent().find("div").toggle();
});
});
Upvotes: 4