Unknown Error Unknown Error - 6 months ago 16
jQuery Question

Script not working when I place jquery.js on footer

I place

jquery.js
on my footer before
</body>


<script type="text/javascript" src="/js/jquery.js"></script>


And why this code is not firing? But when I move jquery.js on header before
</head>
it working fine..

$(document).ready(function () {
$("#sub-category").hide();
$("#main-category").change(function () {
var id = $(this).val();
var dataString = 'id=' + id;
$.ajax({
type: "POST",
url: "/select-category/",
data: dataString,
cache: false,
success: function (html) {
$("#sub-category").show();
$("#sub-category").html(html);
}
});
});
});


Let me know why code above is not firing when I include
jquery.js
on my footer.

Answer

I bet you actually put the $(document).ready(function () { }); block before jQuery library. jQuery has to be loaded even before $(document).ready(function () { }); block in order to make it work. However, in all cases, keep jQuery in <head>, that's the right place for it

Comments