You are on page 1of 24

Chapter_7 Web Programming GTC 2014 E.

Chapter 7: Introduction to web development frameworks

7.1. Bootstrap & jQuery

Bootstrap is a free and open-source CSS framework directed at responsive, mobile-first front-end
web development. It contains CSS- and (optionally) JavaScript-based design templates for typography,
forms, buttons, navigation, and other interface components.

Bootstrap is a free and open-source CSS framework directed at responsive, mobile-first front-end web
development. It contains CSS- and (optionally) JavaScript-based design templates
for typography, forms, buttons, navigation, and other interface components.

As of August 2021, Bootstrap is the tenth most starred project on GitHub, with over 152,000 stars,
behind freeCodeCamp (over 328,000 stars), Vue.js framework, React library, TensorFlow and

Advantages of Bootstrap:

 Easy to use: Anybody with just basic knowledge of HTML and CSS can start using
Bootstrap
 Responsive features: Bootstrap's responsive CSS adjusts to phones, tablets, and
desktops
 Mobile-first approach: In Bootstrap 3, mobile-first styles are part of the core framework
 Browser compatibility: Bootstrap is compatible with all modern browsers (Chrome,
Firefox, Internet Explorer, Edge, Safari, and Opera)

Create First Web Page with Bootstrap

1. Add the HTML5 doctype

Bootstrap uses HTML elements and CSS properties that require the HTML5 doctype.

Always include the HTML5 doctype at the beginning of the page, along with the lang
attribute and the correct character set:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
</html>
Chapter_7 Web Programming GTC 2014 E.C

<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-
beta.2/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.6/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-
beta.2/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
<h2>Alerts</h2>
<div class="alert alert-success">
<strong>Success!</strong> Used to indicate successful or positive action.
</div>
<div class="alert alert-info">
<strong>Info!</strong> Used to indicate a neutral informative change or action.
</div>
<div class="alert alert-warning">
<strong>Warning!</strong> Used to indicate a warning that might need attention.
</div>
<div class="alert alert-danger">
<strong>Danger!</strong> Used to indicate a dangerous or potentially negative action.
</div>
<div class="alert alert-primary">
<strong>Primary!</strong> Used to indicate an important action.
</div>
<div class="alert alert-secondary">
<strong>Secondary!</strong> Used to indicate a slightly less important action.
</div>
<div class="alert alert-dark">
<strong>Dark!</strong> Dark grey alert.
</div>
<div class="alert alert-light">
<strong>Light!</strong> Light grey alert.
</div>
</div>

</body>
</html>
Chapter_7 Web Programming GTC 2014 E.C

Buttons
Use Bootstrap’s custom button styles for actions in forms, dialogs, and more with support for
multiple sizes, states, and more.

Examples

 Bootstrap includes several predefined button styles, each serving its own semantic
purpose, with a few extras thrown in for more control.

Primary ,Secondary, Success, Danger ,Warning ,Info, Light, Dark ,Link

The following example shows the code for the different button styles:

<button type="button" class="btn btn-primary">Primary</button>


<button type="button" class="btn btn-secondary">Secondary</button>
<button type="button" class="btn btn-success">Success</button>
<button type="button" class="btn btn-danger">Danger</button>
<button type="button" class="btn btn-warning">Warning</button>
<button type="button" class="btn btn-info">Info</button>
<button type="button" class="btn btn-light">Light</button>
<button type="button" class="btn btn-dark">Dark</button>
<button type="button" class="btn btn-link">Link</button>

The following example shows the code for the different button styles:
Button Sizes
Bootstrap provides four button sizes:Large Normal Small XSmall
The classes that define the different sizes are:

 .btn-lg
 .btn-sm
 .btn-xs

The following example shows the code for different button sizes:

Example
<button type="button" class="btn btn-primary btn-lg">Large</button>
<button type="button" class="btn btn-primary">Normal</button>
<button type="button" class="btn btn-primary btn-sm">Small</button>
<button type="button" class="btn btn-primary btn-xs">XSmall</button>

Alerts
Provide contextual feedback messages for typical user actions with the handful of available and
flexible alert messages.

Examples
Chapter_7 Web Programming GTC 2014 E.C

Alerts are available for any length of text, as well as an optional close button. For proper styling,
use one of the eight required contextual classes (e.g., .alert-success). For inline dismissal, use
the alerts JavaScript plugin.

A simple primary alert—check it out!

A simple secondary alert—check it out!

A simple success alert—check it out!

A simple danger alert—check it out!

A simple warning alert—check it out!

A simple info alert—check it out!

A simple light alert—check it out!

A simple dark alert—check it out!

<div class="alert alert-primary" role="alert">


A simple primary alert—check it out!
</div>
<div class="alert alert-secondary" role="alert">
A simple secondary alert—check it out!
</div>
<div class="alert alert-success" role="alert">
A simple success alert—check it out!
</div>
<div class="alert alert-danger" role="alert">
A simple danger alert—check it out!
</div>
<div class="alert alert-warning" role="alert">
A simple warning alert—check it out!
</div>
<div class="alert alert-info" role="alert">
A simple info alert—check it out!
</div>
<div class="alert alert-light" role="alert">
A simple light alert—check it out!
</div>
<div class="alert alert-dark" role="alert">
A simple dark alert—check it out!
</div>
Chapter_7 Web Programming GTC 2014 E.C

Text
Documentation and examples for common text utilities to control alignment, wrapping, weight,
and more.

Text alignment

Easily realign text to components with text alignment classes. For start, end, and center
alignment, responsive classes are available that use the same viewport width breakpoints as the
grid system.

<p class="text-start">Start aligned text on all viewport sizes.</p>


<p class="text-center">Center aligned text on all viewport sizes.</p>
<p class="text-end">End aligned text on all viewport sizes.</p>

<p class="text-sm-start">Start aligned text on viewports sized SM (small) or wider.</p>


<p class="text-md-start">Start aligned text on viewports sized MD (medium) or wider.</p>
<p class="text-lg-start">Start aligned text on viewports sized LG (large) or wider.</p>
<p class="text-xl-start">Start aligned text on viewports sized XL (extra-large) or wider.</p>
Note that we don’t provide utility classes for justified text. While, aesthetically, justified text might look
more appealing, it does make word-spacing more random and therefore harder to read.

Text wrapping and overflow


Wrap text with a .text-wrap class.This text should wrap.

<div class="badge bg-primary text-wrap" style="width: 6rem;">


This text should wrap.
</div>
Prevent text from wrapping with a .text-nowrap class.
Chapter_7 Web Programming GTC 2014 E.C

<div class="text-nowrap bd-highlight" style="width: 8rem;">


This text should overflow the parent.
</div>

Word break
Prevent long strings of text from breaking your components' layout by using .text-break to
set word-wrap: break-word and word-break: break-word. We use word-wrap instead of the more
common overflow-wrap for wider browser support, and add the deprecated word-break: break-
word to avoid issues with flex containers.

mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
Copy

<p class="text-
break">mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm</p>
Note that breaking words isn’t possible in Arabic, which is the most used RTL language. Therefore .text-
break is removed from our RTL compiled CSS.

Text transform
Transform text in components with text capitalization classes.

<p class="text-lowercase">Lowercased text.</p>


<p class="text-uppercase">Uppercased text.</p>
<p class="text-capitalize">CapiTaliZed text.</p>
Note how .text-capitalize only changes the first letter of each word, leaving the case of any other
letters unaffected.

Font size
Quickly change the font-size of text. While our heading classes (e.g., .h1–.h6) apply font-size, font-
weight, and line-height, these utilities only apply font-size. Sizing for these utilities matches
HTML’s heading elements, so as the number increases, their size decreases.

<p class="fs-1">.fs-1 text</p>


<p class="fs-2">.fs-2 text</p>
<p class="fs-3">.fs-3 text</p>
<p class="fs-4">.fs-4 text</p>
<p class="fs-5">.fs-5 text</p>
<p class="fs-6">.fs-6 text</p>
Customize your available font-sizes by modifying the $font-sizes Sass map.

Font weight and italics


Chapter_7 Web Programming GTC 2014 E.C

Quickly change the font-weight or font-style of text with these utilities. font-style utilities are
abbreviated as .fst-* and font-weight utilities are abbreviated as .fw-*.

Text with normal font style


<p class="fw-bold">Bold text.</p>
<p class="fw-bolder">Bolder weight text (relative to the parent element).</p>
<p class="fw-normal">Normal weight text.</p>
<p class="fw-light">Light weight text.</p>
<p class="fw-lighter">Lighter weight text (relative to the parent element).</p>
<p class="fst-italic">Italic text.</p>
<p class="fst-normal">Text with normal font style</p>

Line height
Change the line height with .lh-* utilities.

This is a long paragraph written to show how the line-height of an element is affected by our
utilities. Classes are applied to the element itself or sometimes the parent element. These classes
can be customized as needed with our utility API.

This is a long paragraph written to show how the line-height of an element is affected by our
utilities. Classes are applied to the element itself or sometimes the parent element. These classes
can be customized as needed with our utility API.

This is a long paragraph written to show how the line-height of an element is affected by our
utilities. Classes are applied to the element itself or sometimes the parent element. These classes
can be customized as needed with our utility API.

This is a long paragraph written to show how the line-height of an element is affected by our
utilities. Classes are applied to the element itself or sometimes the parent element. These classes
can be customized as needed with our utility API.
<p class="lh-1">This is a long paragraph written to show how the line-height of an element is
affected by our utilities. Classes are applied to the element itself or sometimes the parent
element. These classes can be customized as needed with our utility API.</p>
<p class="lh-sm">This is a long paragraph written to show how the line-height of an element is
affected by our utilities. Classes are applied to the element itself or sometimes the parent
element. These classes can be customized as needed with our utility API.</p>
<p class="lh-base">This is a long paragraph written to show how the line-height of an element is
affected by our utilities. Classes are applied to the element itself or sometimes the parent
element. These classes can be customized as needed with our utility API.</p>
<p class="lh-lg">This is a long paragraph written to show how the line-height of an element is
affected by our utilities. Classes are applied to the element itself or sometimes the parent
element. These classes can be customized as needed with our utility API.</p>

Monospace
Chapter_7 Web Programming GTC 2014 E.C

Change a selection to our monospace font stack with .font-monospace.

This is in monospace
Copy

<p class="font-monospace">This is in monospace</p>

Reset color

Reset a text or link’s color with .text-reset, so that it inherits the color from its parent.

<p class="text-muted">
Muted text with a <a href="#" class="text-reset">reset link</a>.
</p>

Text decoration

 Decorate text in components with text decoration classes.


 This text has a line underneath it.
 This text has a line going through it.
<p class="text-decoration-underline">This text has a line underneath it.</p>
<p class="text-decoration-line-through">This text has a line going through it.</p>
<a href="#" class="text-decoration-none">This link has its text decoration removed</a>
Colors
Convey meaning through color with a handful of color utility classes. Includes support for styling
links with hover states, too.

Colors

Colorize text with color utilities. If you want to colorize links, you can use the .link-* helper
classes which have :hover and :focus states.

<p class="text-primary">.text-primary</p>
<p class="text-secondary">.text-secondary</p>
<p class="text-success">.text-success</p>
<p class="text-danger">.text-danger</p>
<p class="text-warning bg-dark">.text-warning</p>
<p class="text-info bg-dark">.text-info</p>
<p class="text-light bg-dark">.text-light</p>
<p class="text-dark">.text-dark</p>
<p class="text-body">.text-body</p>
<p class="text-muted">.text-muted</p>
<p class="text-white bg-dark">.text-white</p>
<p class="text-black-50">.text-black-50</p>
Chapter_7 Web Programming GTC 2014 E.C

<p class="text-white-50 bg-dark">.text-white-50</p>


Deprecation: With the addition of .text-opacity-* utilities and CSS variables for text
utilities, .text-black-50 and .text-white-50 are deprecated as of v5.1.0. They’ll be removed in
v6.0.0.

Conveying meaning to assistive technologies


Using color to add meaning only provides a visual indication, which will not be conveyed to
users of assistive technologies – such as screen readers. Ensure that information denoted by the
color is either obvious from the content itself (e.g. the visible text), or is included through
alternative means, such as additional text hidden with the .visually-hidden class.
Background
Convey meaning through background-color and add decoration with gradients.

Background color
Similar to the contextual text color classes, set the background of an element to any contextual
class. Background utilities do not set color,
Chapter_7 Web Programming GTC 2014 E.C

<div class="p-3 mb-2 bg-primary text-white">.bg-primary</div>


<div class="p-3 mb-2 bg-secondary text-white">.bg-secondary</div>
<div class="p-3 mb-2 bg-success text-white">.bg-success</div>
<div class="p-3 mb-2 bg-danger text-white">.bg-danger</div>
<div class="p-3 mb-2 bg-warning text-dark">.bg-warning</div>
<div class="p-3 mb-2 bg-info text-dark">.bg-info</div>
<div class="p-3 mb-2 bg-light text-dark">.bg-light</div>
<div class="p-3 mb-2 bg-dark text-white">.bg-dark</div>
<div class="p-3 mb-2 bg-body text-dark">.bg-body</div>
<div class="p-3 mb-2 bg-white text-dark">.bg-white</div>
<div class="p-3 mb-2 bg-transparent text-dark">.bg-transparent</div>

Background gradient
By adding a .bg-gradient class, a linear gradient is added as background image to the backgrounds.
This gradient starts with a semi-transparent white which fades out to the bottom.
Chapter_7 Web Programming GTC 2014 E.C

Tables
Documentation and examples for opt-in styling of tables (given their prevalent use in JavaScript
plugins) with Bootstrap.

Overview
Due to the widespread use of <table> elements across third-party widgets like calendars and date
pickers, Bootstrap’s tables are opt-in. Add the base class .table to any <table>, then extend with
our optional modifier classes or custom styles. All table styles are not inherited in Bootstrap,
meaning any nested tables can be styled independent from the parent.

Using the most basic table markup, here’s how .table-based tables look in Bootstrap.

# First Last Handle

1 Mark Otto @mdo

2 Jacob Thornton @fat

3 Larry the Bird @twitter

<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">First</th>
<th scope="col">Last</th>
<th scope="col">Handle</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>Mark</td>
<td>Otto</td>
<td>@mdo</td>
</tr>
<tr>
<th scope="row">2</th>
<td>Jacob</td>
Chapter_7 Web Programming GTC 2014 E.C

<td>Thornton</td>
<td>@fat</td>
</tr>
<tr>
<th scope="row">3</th>
<td colspan="2">Larry the Bird</td>
<td>@twitter</td>
</tr>
</tbody>
</table>

Variants

Use contextual classes to color tables, table rows or individual cells.

<!-- On tables -->


<table class="table-primary">...</table>
<table class="table-secondary">...</table>
<table class="table-success">...</table>
<table class="table-danger">...</table>
<table class="table-warning">...</table>
<table class="table-info">...</table>
<table class="table-light">...</table>
<table class="table-dark">...</table>

<!-- On rows -->


<tr class="table-primary">...</tr>
Chapter_7 Web Programming GTC 2014 E.C

<tr class="table-secondary">...</tr>
<tr class="table-success">...</tr>
<tr class="table-danger">...</tr>
<tr class="table-warning">...</tr>
<tr class="table-info">...</tr>
<tr class="table-light">...</tr>
<tr class="table-dark">...</tr>

<!-- On cells (`td` or `th`) -->


<tr>
<td class="table-primary">...</td>
<td class="table-secondary">...</td>
<td class="table-success">...</td>
<td class="table-danger">...</td>
<td class="table-warning">...</td>
<td class="table-info">...</td>
<td class="table-light">...</td>
<td class="table-dark">...</td>
</tr>
Conveying meaning to assistive technologies
Using color to add meaning only provides a visual indication, which will not be conveyed to
users of assistive technologies – such as screen readers. Ensure that information denoted by the
color is either obvious from the content itself (e.g. the visible text), or is included through
alternative means, such as additional text hidden with the .visually-hidden class.

Accented tables

Striped rows
Use .table-striped to add zebra-striping to any table row within the <tbody>.

<table class="table table-striped">


...
</table>
These classes can also be added to table variants:

<table class="table table-dark table-striped">


...
</table>

<table class="table table-success table-striped">


...
</table>
Chapter_7 Web Programming GTC 2014 E.C

Hoverable rows
Add .table-hover to enable a hover state on table rows within a <tbody>.

Copy
<table class="table table-hover">
...
</table>

<table class="table table-dark table-hover">


...
</table>
These hoverable rows can also be combined with the striped variant:

Copy

<table class="table table-striped table-hover">


...
</table>

Active tables
Highlight a table row or cell by adding a .table-active class.

<table class="table">
<thead>
...
</thead>
<tbody>
<tr class="table-active">
...
</tr>
<tr>
...
</tr>
<tr>
<th scope="row">3</th>
<td colspan="2" class="table-active">Larry the Bird</td>
<td>@twitter</td>
</tr>
</tbody>
</table>

<table class="table table-dark">


<thead>
...
</thead>
<tbody>
<tr class="table-active">
...
Chapter_7 Web Programming GTC 2014 E.C

</tr>
<tr>
...
</tr>
<tr>
<th scope="row">3</th>
<td colspan="2" class="table-active">Larry the Bird</td>
<td>@twitter</td>
</tr>
</tbody>
</table
What is jQuery?
 jQuery is a fast, small, and feature-rich JavaScript library. It makes things like HTML
document traversal and manipulation, event handling, animation, and Ajax much simpler with
an easy-to-use. With a combination of versatility and extensibility, jQuery has changed the way
that millions of people write JavaScript.

 jQuery is an open-sourced JavaScript library that simplifies creation and navigation


of web applications. Specifically, jQuery simplifies HTML Document Object Model
(DOM) manipulation, Asynchronous JavaScript and XML (Ajax) and event handling.
 jQuery is a lightweight, "write less, do more", JavaScript library.
 The purpose of jQuery is to make it much easier to use JavaScript on your website.
 jQuery takes a lot of common tasks that require many lines of JavaScript code to
accomplish, and wraps them into methods that you can call with a single line of code.
 jQuery also simplifies a lot of the complicated things from JavaScript, like AJAX calls
and DOM manipulation.

The jQuery library contains the following features:

 HTML/DOM manipulation
 CSS manipulation
 HTML event methods
 Effects and animations
 AJAX
 Utilities

Will jQuery work in all browsers?

The jQuery team knows all about cross-browser issues, and they have written this knowledge into the
jQuery library. jQuery will run exactly the same in all major browsers.

Adding jQuery to Your Web Pages

There are several ways to start using jQuery on your web site. You can:

 Download the jQuery library from jQuery.com


 Include jQuery from a CDN, like Google
Chapter_7 Web Programming GTC 2014 E.C

The jQuery library is a single JavaScript file, and you reference it with the HTML <script> tag
(notice that the <script> tag should be inside the <head> section):

<head>
<script src="jquery-3.5.1.min.js"></script>
</head>

Tip: Place the downloaded file in the same directory as the pages where you wish to use it.

jQuery CDN
If you don't want to download and host jQuery yourself, you can include it from a CDN (Content
Delivery Network).

Google is an example of someone who host jQuery:

Google CDN:

<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>

jQuery Syntax
The jQuery syntax is tailor-made for selecting HTML elements and performing some action on the
element(s).

Basic syntax is: $(selector).action()

 A $ sign to define/access jQuery


 A (selector) to "query (or find)" HTML elements
 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".


Chapter_7 Web Programming GTC 2014 E.C

Are you familiar with CSS selectors?

jQuery uses CSS syntax to select elements. You will learn more about the selector syntax in the next
chapter of this tutorial.

The Document Ready Event


You might have noticed that all jQuery methods in our examples, are inside a document ready event:

$(document).ready(function(){

// jQuery methods go here...

});

This is to prevent any jQuery code from running before the document is finished loading (is ready).

It is good practice to wait for the document to be fully loaded and ready before working with it. This
also allows you to have your JavaScript code before the body of your document, in the head section.

Here are some examples of actions that can fail if methods are run before the document is fully
loaded:

 Trying to hide an element that is not created yet


 Trying to get the size of an image that is not loaded yet

Tip: The jQuery team has also created an even shorter method for the document ready event:

$(function(){

// jQuery methods go here...

});

jQuery Selectors

 jQuery selectors allow you to select and manipulate HTML element(s).

 jQuery selectors are used to "find" (or select) HTML elements based on their name, id,
classes, types, attributes, values of attributes and much more. It's based on the existing CSS
Selectors, and in addition, it has some own custom selectors.

 All selectors in jQuery start with the dollar sign and parentheses: $().
Chapter_7 Web Programming GTC 2014 E.C

The element Selector

The jQuery element selector selects elements based on the element name.

You can select all <p> elements on a page like this:

$("p")

Example

When a user clicks on a button, all <p> elements will be hidden:

Example
$(document).ready(function(){
$("button").click(function(){
$("p").hide();
});
});

The #id Selector

 The jQuery #id selector uses the id attribute of an HTML tag to find the specific element.

 An id should be unique within a page, so you should use the #id selector when you want to
find a single, unique element.

 To find an element with a specific id, write a hash character, followed by the id of the HTML
element:

$("#test")

Example

When a user clicks on a button, the element with id="test" will be hidden:

Example
$(document).ready(function(){
$("button").click(function(){
$("#test").hide();
});
});
Chapter_7 Web Programming GTC 2014 E.C

The .class Selector


 The jQuery .class selector finds elements with a specific class.
 To find elements with a specific class, write a period character, followed by the name of the
class:
$(".test")

Example

When a user clicks on a button, the elements with class="test" will be hidden:

Example
$(document).ready(function(){
$("button").click(function(){
$(".test").hide();
});
});

More Examples of jQuery Selectors


Syntax Description

$("*") Selects all elements

$(this) Selects the current HTML element

$("p.intro") Selects all <p> elements with class="intro"

$("p:first") Selects the first <p> element

$("ul li:first") Selects the first <li> element of the first <ul>

$("ul li:first-child") Selects the first <li> element of every <ul>

$("[href]") Selects all elements with an href attribute

$("a[target='_blank']") Selects all <a> elements with a target attribute value equal to "_blank"

$("a[target!='_blank']") Selects all <a> elements with a target attribute value NOT equal to "_blank"

$(":button") Selects all <button> elements and <input> elements of type="button"

$("tr:even") Selects all even <tr> elements

$("tr:odd") Selects all odd <tr> elements


Chapter_7 Web Programming GTC 2014 E.C

Use our jQuery Selector Tester to demonstrate the different selectors.

For a complete reference of all the jQuery selectors, please go to our jQuery Selectors Reference.

Functions In a Separate File

If your website contains a lot of pages, and you want your jQuery functions to be easy to maintain,
you can put your jQuery functions in a separate .js file.

When we demonstrate jQuery in this tutorial, the functions are added directly into
the <head> section. However, sometimes it is preferable to place them in a separate file, like this
(use the src attribute to refer to the .js file):

Example
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="my_jquery_functions.js"></script>
</head>

jQuery Event Methods

jQuery is tailor-made to respond to events in an HTML page.

What are Events?


 All the different visitors' actions that a web page can respond to are called events.
 An event represents the precise moment when something happens.

Examples:

 moving a mouse over an element


 selecting a radio button
 clicking on an element

The term "fires/fired" is often used with events. Example: "The keypress event is fired, the moment
you press a key".

Here are some common DOM events:

Mouse Events Keyboard Events Form Events Document/Windo

click keypress submit load

dblclick keydown change resize


Chapter_7 Web Programming GTC 2014 E.C

mouseenter keyup focus scroll

mouseleave blur unload

jQuery Syntax For Event Methods


 In jQuery, most DOM events have an equivalent jQuery method.
 To assign a click event to all paragraphs on a page, you can do this:
$("p").click();

The next step is to define what should happen when the event fires. You must pass a function to the
event:

$("p").click(function(){
// action goes here!!
});

Commonly Used jQuery Event Methods

$(document).ready()

 The $(document).ready() method allows us to execute a function when the document is fully
loaded. click()

The click() method attaches an event handler function to an HTML element.

 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:

Example
$("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:
Example
$("p").dblclick(function(){
$(this).hide();
});
Chapter_7 Web Programming GTC 2014 E.C

mouseenter()

 The mouseenter() method attaches an event handler function to an HTML element.


 The function is executed when the mouse pointer enters the HTML element:
Example
$("#p1").mouseenter(function(){
alert("You entered p1!");
});

mouseleave()

 The mouseleave() method attaches an event handler function to an HTML element.


 The function is executed when the mouse pointer leaves the HTML element:
Example
$("#p1").mouseleave(function(){
alert("Bye! You now leave p1!");
});

mousedown()

 The mousedown() method attaches an event handler function to an HTML element.


 The function is executed, when the left, middle or right mouse button is pressed down,
while the mouse is over the HTML element:
Example
$("#p1").mousedown(function(){
alert("Mouse down over p1!");
});

mouseup()

 The mouseup() method attaches an event handler function to an HTML element.


 The function is executed, when the left, middle or right mouse button is released, while
the mouse is over the HTML element:
Example
$("#p1").mouseup(function(){
alert("Mouse up over p1!");
});

hover()
Chapter_7 Web Programming GTC 2014 E.C

 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:
Example
$("#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:
Example
$("input").focus(function(){
$(this).css("background-color", "#cccccc");
});

blur()

 The blur() method attaches an event handler function to an HTML form field.
 The function is executed when the form field loses focus:
Example
$("input").blur(function(){
$(this).css("background-color", "#ffffff");
});

The on() Method


 The on() method attaches one or more event handlers for the selected elements.
 Attach a click event to a <p> element:
Example
$("p").on("click", function(){
$(this).hide();
});

Attach multiple event handlers to a <p> element:


Chapter_7 Web Programming GTC 2014 E.C

Example
$("p").on({
mouseenter: function(){
$(this).css("background-color", "lightgray");
},
mouseleave: function(){
$(this).css("background-color", "lightblue");
},
click: function(){
$(this).css("background-color", "yellow");
}
});

You might also like