Published Tuesday 21st January 2014
So, whilst I was at university I adopted a want for learning about how all this programming jazz worked. I remember working a weekend straight, completing an assignment (wrongly and badly coded), then scrapping the entirety of my code and starting again. This happened to me on more than one occasion! When I heard about jQuery, I didn't really understand why it was used, what it was used for and why someone would choose to use it. I wanted to learn all this coding, not use some library to bypass it. However, now that I do understand, I wanted to share what I've learnt with whoever may read this post.
jQuery, or $ for shorthand, is a javascript (JS) library with a lot of built in functions that are designed to make client side scripting (e.g. the animated parts of a website, the instantly refreshing parts of the website) a lot easier. I would recommend still practicing coding in general (PHP, Java, C++, C# etc) but using jQuery to speed up development.
jQuery takes CSS selectors and uses the document object model (DOM) to turn the selector into a JS object. This object will now have the functionality of jQuery.
So to include this powerful library you simply put this script inside your <head>:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
Replace 1.8 with the version you wish to include, but we usually use 1.8 as it supports older versions of Internet Explorer. Now onto the deconstruction of a simple jQuery script:
$('#selector').click(function(e){
//These are comments and will be ignored
//Looks complicated already!!
});
Phew! That was a long explanation. Another thing to note is jQuery should always go inside $(document).ready(function(){ }); which I will show below. This, like the click function, works when the document (i.e. the page you're on) is completely loaded and jQuery is highly dependent upon it. Here are some quality examples of how you may use jQuery:
$(document).ready(function(){
$('#myID').click(function(){
//These are comments and will be ignored
//This below statement can be written as is
$('#myID').slideToggle(); //Hides or shows with animation
//Or it can be written like:
$(this).slideToggle();
//$(this) will select the element that jQuery
//currently has
});
$('.myClass').each(function(count){
//Get the 3rd .myClass, remember
//code starts at 0, not 1
if(count == 2){
$(this).hide(); //Hides without any animation
}
});
//Apply 20 pixel padding to td elements
//on page load
$('table.mySuperTable tr td').css('padding', '20px');
});
I hope this tutorial has helped someone. Please be sure to expand further by using the comment section below!
Blog posts are written by individuals and do not necessarily depict the opinions or beliefs of QWeb Ltd or its current employees. Any information provided here might be biased or subjective, and might become out of date.
Nobody has commented yet.
Your email address is used to notify you of new comments to this thread, and also to pull your Gravatar image. Your name, email address, and message are stored as encrypted text. You won't be added to any mailing list, and your details won't be shared with any third party.