jQuery tutorial for beginners
Author
May 24, 2016
What is jQuery?
jQuery is a lightweight, “write less, do more”, JavaScript library. jQuery is a fast, small, and feature-rich JavaScript library. It makes HTML document traversal and manipulation, event handling, animation, and Ajax much simpler with an easy-to-use API that works across a multitude of browsers. jQuery is absolutely amazing. It’s cross-browser, easy to learn, and makes adding interactivity to your website a breeze.
How to setup jQuery file with html
Before we can do anything, we need to establish our file and link it to our HTML document. First, we will create our plugin file and put it in the directory of our website. It is traditional to start our filename with jQuery, followed by the actual plugin name, so we will call this jquery-hello.
Let us create an index/ file and a folder by the name of js.
jQuery CDN
If you don’t want to download and host jQuery yourself, you can include it from a CDN (Content Delivery Network).
<script src=”https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js”></script>
jQuery Syntax
Basic syntax is: (selector).action()
1. A sign to define/access jQuery
2. A (selector) to “query (or find)” HTML elements
3. A jQuery action() to be performed on the element(s)
Examples:
(this).hide() – hides the current element.
-(“p”).hide() – hides all <p> elements.
-(“.test”).hide() – hides all elements with class=”test”.
-(“#test”).hide() – hides the element with id=”test”.
jQuery Event
Mouse Events | Keyboard Events | Form Events | Document/Window Events |
Click | Keypress | submit | load |
Dblclick | Keydown | change | resize |
Mouseenter | Keyup | focus | scroll |
Mouseleave | blur | unload |
jQuery Events
click()
The function is executed when the user clicks on the HTML element.
The following example says: When a click event fires on a <p> element; hide the current <p> element:
$(“p”).click(function(){
$(this).hide();
});
dblclick()
The dblclick() method attaches an event handler function to an HTML element.
The function is executed when the user double-clicks on the HTML element:
$(“p”).dblclick(function(){
$(this).hide();
});
mouseenter()
The mouseenter() method also attaches an event handler function to an HTML element.
The function is executed when the mouse pointer enters the HTML element:
$(“#p1”).mouseenter(function(){
alert(“You entered p1!”);
});
mouseleave()
The mouseleave() method too attaches an event handler function to an HTML element.
The function is executed when the mouse pointer leaves the HTML element:
$(“#p1”).mouseleave(function(){
alert(“Bye! You now leave p1!”);
});
mousedown()
The event handler attached by mousedown()
is executed, when the left, middle or right mouse button is pressed down, while the mouse is over the HTML element:
$(“#p1”).mousedown(function(){
alert(“Mouse down over p1!”);
});
mouseup()
The event handler attached by mouseup ()
is executed, when the left, middle or right mouse button is pressed down, while the mouse is over the HTML element:
$(“#p1”).mouseup(function(){
alert(“Mouse up over p1!”);
});
hover()
The hover() method takes two functions and is a combination of the mouseenter() and mouseleave() methods.
The first function is executed when the mouse enters the HTML element, and the second function is executed when the mouse leaves the HTML element:
$(“#p1”).hover(function(){
alert(“You entered p1!”);
},
function(){
alert(“Bye! You now leave p1!”);
});
focus()
The focus() method attaches an event handler function to an HTML form field.
The function is executed when the form field gets focus:
$(“input”).focus(function(){
$(this).css(“background-color”, “#cccccc”);
});
blur()
The blur() method too attaches an event handler function to an HTML form field.
The function is executed when the form field loses focus:
$(“input”).blur(function(){
$(this).css(“background-color”, “#ffffff”);
});
On()
The on() method attaches one or more event handlers for the selected elements.
Attach a click event to a <p> element:
$(“p”).on(“click”, function(){
$(this).hide();
});
Attach multiple event handlers to a <p> element:
$(“p”).on(
{ mouseenter: function(){
$(this).css(“background-color”, “lightgray”);
},
mouseleave: function(){
$(this).css(“background-color”, “lightblue”);
},
click: function(){
$(this).css(“background-color”, “yellow”);
}
});
Recent Posts
-
Mirketa Unveils Next-Gen AI Solutions to Redefine the Future of Work Across Industries29 Jul 2025 Press Release
-
Salesforce Implementation School Universities Higher Education23 Jul 2025 Blog
-
Salesforce Health Cloud Implementation Partner: A Complete Guide23 Jul 2025 Blog
-
XML Parsing: Using MINIDOM Vs Element Tree (etree) in Python02 Jul 2025 Blog
-
A step by step Guide to create Salesforce web-to-lead form30 Jun 2025 Blog
-
How AI is Transforming User Experience Design in 202526 Jun 2025 Blog
-
How a Salesforce NPSP Consultant Can Elevate Nonprofit Impact25 Jun 2025 Blog
-
Salesforce Load and Performance Testing: Essentials, Importance & Execution23 Jun 2025 Blog
-
Salesforce Website Integration Boost Leads, Automation & Customer Experience11 Jun 2025 Blog
-
Driving Results in Manufacturing with Salesforce Manufacturing Cloud11 Jun 2025 Blog
-
Accelerating Growth with NetSuite SuiteCommerce02 Jun 2025 Blog
-
Salesforce Service Cloud Services streamlining operations29 May 2025 Blog
-
AI for Nonprofits: Mirketa & Exec Precision Webinar27 May 2025 Press Release
-
AI for Nonprofits: Use Cases, Tools & Implementation Strategies20 May 2025 Webinar
-
Javascript Frameworks for Salesforce Lightning Design System18 May 2025 Blog
-
Building a Smart Campus with Salesforce Student Information System: A Road to Smarter Education16 May 2025 Blog
-
Salesforce Nonprofit Cloud: Benefits & Consultant Role15 May 2025 Blog
-
Salesforce Consulting for Nonprofits: Maximize Impact09 May 2025 Blog
-
What to Expect from a Salesforce Admin Service Provider09 May 2025 Blog
-
Maximizing Efficiency with Salesforce Cloud Integration Services09 May 2025 Blog