HTML Guide
HTML Guide
jQuery
Table of contents i
1 HTML 1
1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 First code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.3 Basic tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.4 Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.4.1 Attribute ‘name’ and ‘value’ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.4.2 Core attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.5 Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.6 Text formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.7 Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.8 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.9 Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.10 Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3 Bootstrap 22
3.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
3.2 Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
3.2.1 Download and include files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
3.2.2 Add CDN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3.2.3 Check setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3.3 Grid system . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
3.3.1 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
3.3.2 Nested columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
3.3.3 Offset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
3.4 Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
3.4.1 Labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
3.4.2 Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
3.4.3 Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
3.4.4 Horizontal form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
3.4.5 Form elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
i
Table of contents
4 JavaScript 45
4.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
4.2 First code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
4.2.1 JavaScript in HTML file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
4.3 Keywords, Datatypes, Variables and Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
4.3.1 Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
4.3.2 Datatypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
4.3.3 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
4.3.4 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
4.3.5 String to number conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
4.3.6 Convert to integer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
4.3.7 Convert to float . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
4.3.8 Math . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
4.3.9 String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
4.3.10 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
4.4 Control structure, loops and functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
4.4.1 If-else . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
4.4.2 Switch-case-default . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
4.4.3 For loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
4.4.4 While loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
4.4.5 do-while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
4.4.6 for-in loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
4.4.7 Continue and break . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
4.4.8 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
4.5 Event handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
4.6 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
5 jQuery 56
5.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
5.1.1 Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
5.1.2 Add contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
5.2 jQuery examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
5.2.1 Add jQuery code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
5.2.2 jQuery in separate file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
5.2.3 Get input from user . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
5.3 Selectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
5.3.1 Select elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
5.3.2 Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
5.4 Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
5.5 Event handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
ii PythonDSP
Chapter 1
HTML
1.1 Introduction
In this chapter, various component of HTML are discussed to design a web page.
The basic structure for an HTML page is shown below.
• Entries inside the /< . . . /> are known as tags. Most of the tags has an opening and closing e.g. <head>
(opening head) and </head> (closing head). Some of the tags do not have closing tags e.g. <!DOCTYPE
. . . > and <br />. We need to write the HTML codes inside the tags.
• The comments are written between ‘<!–’ and ‘–>’.
• Here Line 1 gives the details of the ‘HTML version’ to the web-browser. The ‘html’ tells it is version 5.
• The ‘head’ tag (Lines 3-5) contains the header related tags e.g. ‘title for the page’ and ‘links for the css files’
etc.
• The ‘body’ tag (7-11) contains the actual HTML code which is displayed on the web-browser. Also, we add
all the JavaScript related codes just before the closing body tag (</body>).
1 <!DOCTYPE html> <!-- tells browser above the html version -->
2 <html> <!-- beginning of the html document -->
3 <head>
4 <!-- header related tags e.g. title, links etc. -->
5 </head>
6
7 <body>
8 <!-- actual html document here -->
9
In below code, the message “Hello World” is displayed on the HTML page. The Fig. 1.1 is the resultant HTML
page.
• The title (Line 4) appears on the top of the browser.
• The tag <h1> is called ‘header’ tag, which has the larger size than the normal text (see the size of ‘Hello
World!’).
• The tag <p> is called the ‘paragraph’ tag, which can be used to write the paragraphs.
<!DOCTYPE html>
<html>
(continues on next page)
1
1.3. Basic tags
• The Table 1.1 shows the list of tags which are required for writing the basic ‘HTML’ codes i.e. without any
style e.g. bold, italics and numbering etc.
Note: All the new codes are added below the previous codes in the ‘body’ tag. Therefore only newly added
codes are shown in the tutorial.
2 PythonDSP
Chapter 1. HTML
<div style="color:blue;">
The 'div' tag can be used for formatting the tags inside it at once using 'style' and 'classes'␣
˓→etc.
</div>
<center>
<h3> Heading 3 is centered</h3>
<p><span> Centered span inside the paragraph.</span><p>
</center>
<pre> 'pre' tag preserve the formatting (good for writing codes)
# Python code
x = 2
y = 3
print(x+y)
</pre>
• Fig. 1.2 is the output of above code. Read the text to understand each tag,
1.4 Attributes
In Fig. 1.2, we saw an example of attribute (i.e. style) which changed the color of all the elements to ‘blue’ inside
the ‘div’ tag.
• Attribute is defined inside the opening part of a ‘tag’. For example, in the below code, the attribute ‘style’
is defined inside the ‘div’ tag.
<div style="color:blue;">
</div>
• An attribute has two parts i.e. ‘name’ and ‘value’. For example, in the above code, name and value of
the attribute are ‘style’ and ‘blue’ respectively.
Below are the three core attributes which are used frequently in web design.
• id : The ‘id’ is the unique name which can be given to any tag. This is very useful in distinguishing the
element with other elements.
<p id='para1'> This is paragraph with id 'para1' </p>
<p id='para2'> This is paragraph with id 'para2' </p>
• class : The attribute ‘class’ can be used with multiple tags. This is very useful in making groups in HTML
design.
<p class="c_blue"> This is paragraph with class 'blue'</p>
<span class="c_blue"> This is span with class 'blue'</span>
• style : We already see the example of style attribute, which can be used to change the formatting of the
text in HTML design. We can specify various styles which are discussed in Chapter 2.
<p style="font-weight:bold; color:red;">Style attribute is used to bold and color</p>
Note: Above three attributes are used with ‘CSS (cascading style sheet)’ and JavaScript/jQuery, which are the
very handy tools to enhance the look and functionalities of the web-page respectively. The CSS is discussed in
Chapter 2, whereas JavaScript and jQuery are discussed in Chapter 4 and Chapter 5 respectively.
• Also we can define multiple attributes for one tag as shown below,
<p class="my_class" id="para_with_class" style="color:green"> Multiple attributes </p>
4 PythonDSP
Chapter 1. HTML
1.5 Tables
In this section, we will learn to draw tables along with some attributes which are discussed in Table 1.2. Table 1.3
shows the list of tags available to create the table, which are used in Listing 1.1.
22
23
39 </table>
In this section, we will see some of the text formatting options (see Table 1.4) e.g. bold, italic, subscript and strike
etc.
• Below are the some of the examples of text formatting, whose results are shown in Fig. 1.4,
1.7 Images
Image tag has two important attribues i.e. ‘src’ and ‘alt’ as described below,
• src : tells the location of ‘image’ file e.g. in Line 2 the image ‘logo.jpg’ will be searched inside the folder
‘img’.
6 PythonDSP
Chapter 1. HTML
• alt : is the ‘alternate text’ which is displayed if image is not found. For example, in Line 6, the name of the
image is incorrectly written i.e. ‘logoa’ (instead of ‘logo’), therefore the value of ‘alt’ i.e. ‘Missing Logo.jpg’
will be displayed as shown in Fig. 1.5.
4 <br/> <br/>
5
Note: We can use other attributes as well e.g. ‘height’, ‘align’ and ‘border’ etc.
1.8 Lists
1.9 Links
<br>
<p><a href="js.html" target="_self"> JavaScript Tutorial</a> in same window.</p>
<p><a href="js.html" target="_blank"> JavaScript Tutorial</a> in new Window.</p>
(continues on next page)
8 PythonDSP
Chapter 1. HTML
Note: We can change the color of the links using ‘alink (active link)’, ‘link’ and ‘vlink (visited link’, by defining
these attributes in the ‘body tag’ as shown below,
1.10 Forms
Forms can have different types of controls to collect the input-data from users, which are listed below and shown
in Table 1.5,
• Text input
• Text area
• Radio button
• Checkbox
• Select box
• File select
• Buttons
• Submit and reset buttons
• Hidden input
10 PythonDSP
Chapter 1. HTML
• Below are the exmaple of the control inputs described in Table 1.5
</form>
12 PythonDSP
Chapter 1. HTML
14 PythonDSP
Chapter 2
2.1 Introduction
CSS is used to enhance the look of the web page. In Section 1.4.2, we saw the attribute ‘style’, which is used for
changing the color of the text. Let’s rewrite the example of ‘style’ as shown in next section.
• Below code is an example of ‘inline CSS’, where the styles are defined inside the individual tags.
3 <!DOCTYPE html>
4 <html>
5 <head>
6 <title>CSS Tutorial</title>
7
8 </head>
9 <body>
10
15 </body>
16 </html>
In the above code, we have three ‘headings’ with font-color as ‘blue’. Suppose, we want to change the color to red,
then we must go to to individual ‘h3’ tag and then change the color. This is easy in this case, but if we have 100
headings in 5 different ‘html’ files, then this process is not very handy. In such cases, CSS can be quite useful as
shown in next section.
In the below code, the style is embedded inside the ‘style’ tag as shown in Lines 8-17. Here, we have defined two
classes i.e. ‘h3_blue (Lines 21-23)’ and ‘h3_red (Lines 26-28)’. Then, the selectors at Lines 9 and 13 targets the
class ‘h3_blue’ & ‘h3_red’, and change the color to blue and red respectively. In this chapter, we will discuss the
selectors (e.g. h3.h3_blue) in more details.
Note:
• In CSS, the comments are written between /* and */.
15
2.1. Introduction
3 <!DOCTYPE html>
4 <html>
5 <head>
6 <title>CSS Tutorial</title>
7
8 <style type="text/css">
9 h3.h3_blue{ /*change color to blue*/
10 color: blue;
11 }
12
18 </head>
19 <body>
20
25
30 </body>
31 </html>
We can write the ‘CSS’ code in different file and then import the file into ‘html’ document as shown in this section.
In this way, we can manage the files easily.
• The ‘CSS’ code is saved in the file ‘my_css.css’ which is saved inside the folder ‘asset/css’.
/* asset/css/my_css.css */
h3.h3_blue{
color: blue;
}
h3.h3_red{
color:red;
}
• Next, we need to import the CSS file into the ‘html’ file as shown in Line 7.
16 PythonDSP
Chapter 2. Cascading Style Sheets (CSS)
15
20 </body>
21 </html>
3 <!DOCTYPE html>
4 <html>
5 <head>
6 <title>CSS Selectors</title>
7 <link rel="stylesheet" type="text/css" href="asset/css/my_css.css">
8 </head>
9 <body>
10 <h3>CSS Selectors</h3>
11
15 </body>
16 </html>
• Below code shows the example of different selectors, and the output is shown in Fig. 2.2
1 /* asset/css/my_css.css */
2
3 /*element selection*/
4 h3 {
5 color: blue;
6 }
7
9 /*class selection*/
10 .c_head{
11 font-family: cursive;
12 color: orange;
13 }
14
15
16 /*id selection*/
17 #i_head{
18 font-variant: small-caps;
19 color: red;
20 }
2.3 Hierarchy
In previous section, we saw the example of selectors. In this section, we will understand the hierarchy of the
styling-operations.
18 PythonDSP
Chapter 2. Cascading Style Sheets (CSS)
• Priority level :
– ID (highest priority)
– Class
– Element
• If two CSS has same priority, then CSS rule at the last will be applicable.
3 <!DOCTYPE html>
4 <html>
5 <head>
6 <title>CSS Selectors</title>
7 <link rel="stylesheet" type="text/css" href="asset/css/my_css.css">
8 </head>
9 <body>
10 <p>Paragraph</p>
11
15 </body>
16 </html>
Below is the CSS code. Let’s understand the formatting of all three ‘p’ tags individually. The results are shown
in Fig. 2.3.
• ‘p’ tag at Line 13 of html : Since, ‘id’ has highest priority, therefore CSS rule for #i_head’ (Line 12) will
not be overridden by Line 24; hence the color is red. Line 13 has ‘p’ tag, therefore ‘font-variant’ rule will be
applied by Line 17. Also, this tag has class ‘c_head’, therefore ‘font’ will be set to ‘cursive’. Hence, the line
is “all-caps with font-cursive in red color”.
• ‘p’ tag at Line 12 of html : Similarly, the ‘head’ tag has higher priority than ‘element’ therefore color of
this line is oranage and font-family is ‘cursive’. Also, Line 17 will make it all caps
• ‘p’ tag at Line 10 of html : Color defined at Line 18 will be overridden by Line 24; hence the color will
be blue. Also, Line 17 will make it all caps.
1 /* asset/css/my_css.css */
2
3 /*class selection*/
4 .c_head{
5 font-family: cursive;
6 color: orange; /*override the blue color*/
7 }
8
10 /*id selection*/
11 #i_head{
12 color: red;
13 }
14
15 /*element selection*/
16 p {
17 font-variant: small-caps;
18 color: blue;
19 }
20
21
Table 2.1 shows the combinations of selectors to target the various elements of the HTML. Also, some of the
example of ‘Attribute selector’ is shown in this section.
• Add below code at the end of the html file. In these lines ‘custom attributes’ are added (i.e. my_id).
<span my_id='m_span'> Span with attribute 'my_id' with value 'm_span' </span>
<br>
<span my_id='m_span2'> Span with attribute 'my_id' with value 'm_span2' </span>
/*attribute selection*/
span[my_id] { /* select 'span' with attribute 'my_id' */
color: green;
font-weight: bold
(continues on next page)
20 PythonDSP
Chapter 2. Cascading Style Sheets (CSS)
Table 2.2 shows the some more important properties which can be used in CSS,
Bootstrap
3.1 Introduction
One of the problem with basic HTML design is that the webpage may look different in different browser or device
(e.g. mobile, tablet and laptop). Therefore, we may need to modify the code according to browser or device. The
problem can be easily resolved by using Bootstrap.
Bootstrap is a framework which uses HTML, CSS and JavaScript for the web design. It is supported by all the
major browsers e.g. Firefox, Opera and Chrome etc. Further, Bootstrap includes several predefined classes for
easy layouts e.g. dropdown buttons, navigation bar and alerts etc. Lastly, it is responsive in nature i.e. the layout
changes automatically according to the device e.g. mobile or laptop etc.
3.2 Setup
Bootstrap needs atleast 3 files for its operation which can be downloaded from the Bootstrap website.
• bootstrap.css (Line 7) : This file contains various CSS for bootstrap.
• bootstrap.js (Line 16) : This file contains various JavaScript functionalities e.g. dropdown and alerts etc.
• jQuery.js (Line 17) : This file is the jQuery library which can be downloaded from the ‘jQuery’ website.
It is required for proper working of ‘bootstrap.js’.
These files are downloaded and saved inside the ‘asset’ folder. Next, we need to include these files in the HTML
document as below,
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <title>Bootstrap Tutorial</title>
5
10 </head>
11 <body>
12
13
22
Chapter 3. Bootstrap
Another way to include the files is CDN. In this method, we need not to download the files, but provide the links
to these files, as shown in Lines 8, 17 and 19 of below code. Note that, in this case the code will not work in offline
mode.
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <title>Bootstrap Tutorial</title>
5
˓→"anonymous">
11 </head>
12 <body>
13
14
20 </body>
21 </html>
Note: In this tutorial, we have used the first method i.e. download the files and include in the html document.
Let’s write our first code to check the correct setup of the Bootstrap.
Note: We need not to memorize the code for creating the ‘dropdown’ button. All the templates are available on
the Bootstrap website. Copy and paste the code from there and the modify the code according to need as shown
in this tutorial.
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <title>Bootstrap Tutorial</title>
5
10 </head>
11 <body>
12
13 <div class="dropdown">
14 <button class="btn btn-success dropdown-toggle" type="button" id="dropdownMenu1" data-toggle=
˓→"dropdown" aria-haspopup="true" aria-expanded="true">
15 Dropdown
16 <span class="caret"></span>
17 </button>
18 <ul class="dropdown-menu" aria-labelledby="dropdownMenu1">
19 <li><a href="#">Action</a></li>
20 <li><a href="#">Another action</a></li>
21 <li><a href="#">Something else here</a></li>
22 <li role="separator" class="divider"></li>
23 <li><a href="#">Separated link</a></li>
24 </ul>
25 </div>
26
Bootstrap divides the each row into 12 columns. Then following commands can be used to specify the width
the columns
• col-lg-4 : It will select 4 columns. Choose any number between 1-12. The ‘lg’ stand for large screen (e.g.
large computer screen).
• col-md-5 : ‘md’ = medium screen
• col-sm-3 : ‘sm’ = small screen
• col-xs-3 : ‘xs’ = extra small screen
• col-lg-offset-4 : skip first 4 columns. Simimlary use ‘md’, ‘sm’ and ‘xs’ with offset
24 PythonDSP
Chapter 3. Bootstrap
3.3.1 Example
Below is an example of grid system. Read the content of the Lines 13-16 to understand it. The resultant webpage
is shown in Fig. 3.2.
Note:
• For easy visualization, in the below code the CSS code (Lines 9-21) is used to fill the columns with colors
and border.
• The columns (Lines 28-30) should be defined inside the class ‘row’ (Line 27).
• Also, sum of the widths of individual columns should not be greater than 12.
• Lastly, if we use ‘col-md-4’ without defining the ‘lg’, ‘sm’ and ‘xs’, then ‘md’ rule will be applicable to higher
size screen ‘lg’, but not on the lower size screen ‘sm’ and ‘xs’. Similary, if we use ‘sm’ without defining the
‘lg’, ‘md’ and ‘xs’, then rule will be applicable to higher size screen i.e. ‘lg’ and ‘md’ but not on ‘xs’
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <title>Bootstrap Tutorial</title>
5
23 </head>
24
25 <body>
26
27 <div class="row">
28 <div class="col-md-2 col-xs-4">col-md-2, col-xs-4</div>
29 <div class="col-md-6 col-xs-4">col-md-6, col-xs-4</div>
30 <div class="col-md-4 col-xs-4">col-md-4, col-xs-4</div>
31 </div>
32
33
Fig. 3.2: Different grid-size (i.e. 2, 6, 4) for ‘medium’ and ‘large’ screen
Fig. 3.3: Equal grid-size (i.e. 4) for ‘extra small’ and ‘small’ screen
We can further divide a column into small columns by defining a class ‘row (Line 30)’ inside the ‘column’ (Line
29), as shown in Lines 29-34.
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <title>Bootstrap Tutorial</title>
5
23 </head>
24
25 <body>
26
27 <div class="row">
28 <div class="col-md-2 col-xs-4">col-md-2, col-xs-4</div>
29 <div class="col-md-6 col-xs-4">
30 <div class="row">
31 <div class="col-xs-6">col-xs-6</div>
32 <div class="col-xs-6">col-xs-6</div>
33 </div>
34 </div>
35 <div class="col-md-4 col-xs-4">col-md-4, col-xs-4</div>
36 </div>
37
38
3.3.3 Offset
We can skip the columns using ‘offset’ as shown in Line 27; and the corresponding html page is shown in Fig. 3.4,
26 PythonDSP
Chapter 3. Bootstrap
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <title>Bootstrap Tutorial</title>
5
24 <body>
25
26 <div class="row">
27 <div class="col-md-offset-2 col-md-2">col-md-2</div>
28 <div class="col-md-8">col-md-8</div>
29 </div>
30
31
3.4 Components
Once we understand the ‘html’, ‘css’ and ‘Bootstrap-grid’ understood, then the use of Bootstrap-components
are straightforward. Just copy and paste the code from the Bootstrap website and modify it according to the
requirement.
In this section, we will use following template.
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap Tutorial</title>
<!-- CSS -->
<link href="asset/css/bootstrap.min.css" rel="stylesheet">
<!-- Add Custom CSS below -->
</head>
(continues on next page)
<body>
3.4.1 Labels
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <title>Bootstrap Tutorial</title>
5 <!-- CSS -->
6 <link href="asset/css/bootstrap.min.css" rel="stylesheet">
7 <!-- Add Custom CSS below -->
8 </head>
9
10 <body>
11 <h3>Example heading <span class="label label-info">New</span></h3>
12 <hr>
13
28 PythonDSP
Chapter 3. Bootstrap
Note: All the codes will be added in the ‘body’ tag, and the new code will be inserted below the previous codes.
Therefore full HTML code is not added in the tutorial.
3.4.2 Buttons
The element ‘button’ can be used to create the button in Bootstrap as shown below,
Note:
• We can use ‘lg’, ‘md’ and ‘xs’ etc. to set the size of the button.
• In Line 3, a glyphicon is added (i.e. trash sign). More glyphicon can be found on the Boostrap-webpage.
3.4.3 Forms
</div>
(continues on next page)
30 PythonDSP
Chapter 3. Bootstrap
</div>
<div class="checkbox">
<label>
<input type="checkbox"> Remember me
</label>
</div>
<button type="submit" class="btn btn-success">Sign in</button>
</form>
(continues on next page)
Modal form appears in the pop-up window. Output of below code is shown in Fig. 3.10,
<div class="row">
<div class="col-sm-5" style="background: pink">
(continues on next page)
32 PythonDSP
Chapter 3. Bootstrap
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input class="form-control" id="exampleInputPassword3" placeholder="Password
˓→" type="password">
</div>
<div class="form-group">
<label for="exampleInputFile">File input</label>
<input id="exampleInputFile3" type="file">
<p class="help-block">Example block-level help text here.</p>
</div>
<div class="checkbox">
<label>
<input type="checkbox"> Check me out
</label>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
<!-- actual form ends -->
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<hr>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 col-sm-2 control-label">Rounder</label>
<div class="col-sm-10">
<input class="form-control round-input" type="text">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 col-sm-2 control-label">Input focus</label>
(continues on next page)
34 PythonDSP
Chapter 3. Bootstrap
</div>
</div>
<div class="form-group">
<label class="col-sm-2 col-sm-2 control-label">Placeholder</label>
<div class="col-sm-10">
<input class="form-control" placeholder="placeholder" type="text">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 col-sm-2 control-label">Password</label>
<div class="col-sm-10">
<input class="form-control" placeholder="" type="password">
</div>
</div>
<div class="form-group">
<label class="col-lg-2 col-sm-2 control-label">Static control</label>
<div class="col-lg-10">
<p class="form-control-static">[email protected]</p>
</div>
</div>
</form>
</div>
</div>
</div>
36 PythonDSP
Chapter 3. Bootstrap
<h3>Buttons</h3>
<div class="row">
<div class="col-sm-12" style="background: pink">
<div class="panel-body">
<form class="form-horizontal tasi-form" method="get">
<div class="form-group">
<label class="col-sm-2 control-label col-lg-2" for="inputSuccess">Checkboxes and␣
˓→radios</label>
<div class="col-lg-10">
<div class="checkbox">
<label>
<input value="" type="checkbox">
Option one is this and that—be sure to include why it's great
</label>
</div>
<div class="checkbox">
(continues on next page)
<div class="radio">
<label>
<input name="optionsRadios" id="optionsRadios1" value="option1" checked=
˓→"checked" type="radio">
Option one is this and that—be sure to include why it's great
</label>
</div>
<div class="radio">
<label>
<input name="optionsRadios" id="optionsRadios2" value="option2" type=
˓→"radio">
Option two can be something else and selecting it will deselect option␣
˓→one
</label>
</div>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label col-lg-2" for="inputSuccess">Inline checkboxes</
label>
˓→
<div class="col-lg-10">
<label class="checkbox-inline">
<input id="inlineCheckbox1" value="option1" type="checkbox"> 1
</label>
<label class="checkbox-inline">
<input id="inlineCheckbox2" value="option2" type="checkbox"> 2
</label>
<label class="checkbox-inline">
<input id="inlineCheckbox3" value="option3" type="checkbox"> 3
</label>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label col-lg-2" for="inputSuccess">Selects</label>
<div class="col-lg-10">
<select id="select-dropdown-num" class="form-control m-bot15">
<option selected="selected">1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
38 PythonDSP
Chapter 3. Bootstrap
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label col-lg-2">Checkboxe and radio</label>
<div class="col-lg-10">
<div class="input-group m-bot15">
<span class="input-group-addon">
<input type="checkbox">
</span>
<input class="form-control" type="text">
</div>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label col-lg-2">Button addons</label>
<div class="col-lg-10">
<div class="input-group m-bot15">
<span class="input-group-btn">
<button class="btn btn-white" type="button">Go!</button>
</span>
<input class="form-control" type="text">
</div>
(continues on next page)
40 PythonDSP
Chapter 3. Bootstrap
<ul class="dropdown-menu">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">Something else here</a></li>
<li class="divider"></li>
<li><a href="#">Separated link</a></li>
</ul>
</div>
<input class="form-control" type="text">
</div>
<div class="input-group m-bot15">
<input class="form-control" type="text">
<div class="input-group-btn">
<button type="button" class="btn btn-white dropdown-toggle" data-toggle="dropdown
˓→">Action <span class="caret"></span></button>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label col-lg-2">Segmented buttons</label>
<div class="col-lg-10">
<div class="input-group m-bot15">
<div class="input-group-btn">
<button tabindex="-1" class="btn btn-white" type="button">Action</button>
<button tabindex="-1" data-toggle="dropdown" class="btn btn-white dropdown-toggle
˓→" type="button">
<span class="caret"></span>
</button>
<ul role="menu" class="dropdown-menu">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">Something else here</a></li>
<li class="divider"></li>
<li><a href="#">Separated link</a></li>
</ul>
</div>
<input class="form-control" type="text">
</div>
<span class="caret"></span>
</button>
<ul role="menu" class="dropdown-menu pull-right">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">Something else here</a></li>
<li class="divider"></li>
<li><a href="#">Separated link</a></li>
</ul>
</div>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
For navbar, one more ‘css’ file is added at Line 8. This CSS will create the space at the top for the ‘navbar’ so that
it will not overlap with the contents of the HTML page. Further, Lines 13-44 adds various links to the navbar.
Lastly, we can add HTML content below Line 46.
Output of below code is shown in Fig. 3.15,
1 <!DOCTYPE html>
2 <html>
3 <head>
(continues on next page)
42 PythonDSP
Chapter 3. Bootstrap
11 <body>
12
17 <span class="icon-bar"></span>
18 <span class="icon-bar"></span>
19 <span class="icon-bar"></span>
20 </button>
21 <a class="navbar-brand hidden-sm" href="http://pythondsp.readthedocs.io/">PythonDSP</a>
22 </div>
23 <div class="row">
24 <div class="navbar-collapse collapse">
25 <ul class="nav navbar-nav">
26
27 <li><a href="basic.html">HTML</a></li>
28 <li><a href="css.html">CSS</a></li>
29
30
31 <li class="dropdown">
32 <a href="#" class="dropdown-toggle" data-toggle="dropdown">More<b class="caret"></b></a>
33 <ul class="dropdown-menu">
34 <li><a href="bootstrap.html">Bootstrap</a></li>
35 <li><a href="js.html">Javascript</a></li>
36 <li><a href="jquery.html">jQuery</a></li>
37 </ul>
38 </li>
39
40 </ul>
41 </div>
42 </div><!-- /.nav - collapse -->
43 </div>
44 </div>
45
44 PythonDSP
Chapter 4
JavaScript
4.1 Introduction
• JavaScript is a dynamic language which is used for designing the web pages on the client side.
• It is case sensitive language.
• It is untyped language i.e. a variable can hold any type of value.
• // is used for comments.
• ; i used for line termination.
• JavaScript code should be added at the end i.e. just above the closing-body-tag.
• It is better to write the JavaScript code in separate file as shown in next section.
The JavaScript code can be written in the ‘html’ file or in the separate ‘JavaScript file (.js)’ as shown in this
section,
In HTML file, the JavaScript codes can be written inside the ‘script’ tag as shown in Lines 11-13 of below code.
The code will write the message “Hello World from JavaScript!” on the web page. Open the ‘js.html’ in the browser
to see the message.
3 <!DOCTYPE html>
4 <html>
5 <head>
6 <title>JavaScript</title>
7 </head>
8 <body>
9
10
11 <script type="text/javascript">
12 document.write("Hello World from JavaScript!<br>");
13 </script>
14
15 </body>
16 </html>
45
4.3. Keywords, Datatypes, Variables and Operators
• The JavaScript code is saved in the file ‘my_javascript.js’ which is located inside the folders ‘asset/js’. Note
that, no ‘script tag’ is used here.
1 // asset/js/my_javascript.js
2
• Next we need to import the .js file in the HTML file as shown below,
3 <!DOCTYPE html>
4 <html>
5 <head>
6 <title>JavaScript</title>
7 </head>
8 <body>
9
10
11
15 </body>
16 </html>
• Now, open the ‘js.html’ file in the browser and it will display the message.
4.3.1 Keywords
• Below are the reserved keywords in the JavaScript which can not be used as ‘variable’ and ‘function’ names
etc.
46 PythonDSP
Chapter 4. JavaScript
4.3.2 Datatypes
4.3.3 Variables
Variables can be define using keyword ‘var’. Further, JavaScript is untyped language i.e. a variable can hold any
type of value.
• In the below HTML, ‘p’ tag with id ‘p_name’ is defined at Line 10. This id will be used to write some text
using JavaScript,
3 <!DOCTYPE html>
4 <html>
5 <head>
6 <title>JavaScript</title>
7 </head>
8 <body>
9
10 <p id='p_name'></p>
11
15 </body>
16 </html>
• Two variables are defined at Lines 9-10 of type ‘string’ and ‘float’ respectively. Then ‘getElementById’
is used to locate the tag with id ‘p_name’ and the text is inserted as shown in Line 11. Lastly, the ‘+’ sign
is used to concatenate the values as Line 11.
1 // asset/js/my_javascript
2
8 // variable example
9 var your_name = "Meher";
10 var age = 20;
11 document.getElementById("p_name").innerHTML= "Hello "+ your_name + "<br>Age : " + age;
• Below is the output of above codes. Note that the message ‘Hello World from JavaScript!’ is added at the
end as ‘JavaScript’ file is imported at the end.
Warning: If we import the ‘JavaScript’ file between the ‘ending-head-tag’ and ‘start-body-tag’, then Message
‘Hello Meher . . . ’ will not be displayed as ‘JavaScript’ will execute before the loading of the page; and JavaScript
can not find the id ‘p_name’.
Hello Meher
Age : 20
Note: All the JavaScript/HTML codes will be added below the existing codes, therefore only the newly added
code will be shown in the rest of the tutorial.
A pop-up window will appear due to prompt. If we provide the input value as 3, then below output will be
generated,
2 * 3 = 6
Note: The ‘,’ and ‘+’ can be used to combine various outputs as shown in above example
4.3.4 Operators
Various operators are shown in this section. The usage of some of these operators are shown in Section 4.4.
• +
• -
• *
• /
• % : modulus i.e remainder of the division
• ++ (increment)
• – (decrement)
48 PythonDSP
Chapter 4. JavaScript
• =
• +=
• -=
• *=
• /=
• %=
• ?:
e.g. ‘( (a > b) ? a/b : b/a) )’ i.e if ‘a>b’ then do ‘a/b’ else do ‘b/a’
• & (and)
• | (or)
• ^ (xor)
• ~ (not)
2 + Number('3.4') = 5.4
// int conversion
document.write("2 + parseInt('3.4') = ", 2 + parseInt('3.4'), "<br>"); // string to int
document.write("2 + parseInt(3.4) = ", 2 + parseInt(3.4), "<br>"); // float to int
2 + parseInt('3.4') = 5
2 + parseInt(3.4) = 5
The ‘parseFloat’ or ‘Number’ can be used to convert the value into float values.
4.3.8 Math
// math
document.write("pi = ", Math.PI, "<br>");
document.write("e = ", Math.E, "<br>");
document.write("similarly we can use 'abs', 'floor', 'ceil' and 'round' etc. <br>")
document.write("random number : ", Math.ceil(Math.random()*20), "<br>"); // enter random number
pi = 3.141592653589793
e = 2.718281828459045
similarly we can use 'abs', 'floor', 'ceil' and 'round' etc.
random number : 16
4.3.9 String
// string
document.write("meher".toUpperCase(), "<br>") // uppercase
w = "Krishna"
document.write(w.toLowerCase(), "<br>") // lowercase
document.write(w.small(), "<br>") // small
document.write(w.bold(), "<br>") // bold
document.write(w.strike(), "<br>") // strike
document.write(w.fontsize("5em"), "<br>") // strike
document.write(w.link("http://pythondsp.readthedocs.io"), "<br>") // link
4.3.10 Arrays
In JavaScript, the arrays can store different types of values as shown in below code,
50 PythonDSP
Chapter 4. JavaScript
// arrays
arr = [15, 30, "Meher"]
for(a in arr)
document.write(arr[a], " ");
document.write("<br>");
15 30 Meher
Meher
Krishna
lenght of array: 2
4.4.1 If-else
In the below code, three conditions are checked for the variable ‘age’; and corresponding message is printed.
// asset/js/my_javascript
// if-else
age = 10;
if (age > 3 && age < 6){
document.write("Age : " + age + "<b> go to kindergarten</b>");
}
else if ( age >=6 && age < 18){
document.write("Age : " + age + "<b> go to school</b>");
}
else{
document.write("Age : " + age + "<b> go to college</b>");
(continues on next page)
• Since age is 10, therefore ‘else if’ statement is satisfied and we will get below output,
Age : 10 go to school
4.4.2 Switch-case-default
// switch-case
var grade = 'A';
document.write("Grade " + grade + " : ");
switch(grade){
case 'A':
document.write("Very good grade!");
break;
case 'B':
document.write("Good grade!");
break;
default: // if grade is neither 'A' nor 'B'
document.write("Enter correct grade");
}
document.write("<br>");
52 PythonDSP
Chapter 4. JavaScript
4.4.5 do-while
// do-while
x=0;
do{
document.write(x + " ");
x++;
}while(x < 3);
document.write("<br>");
0 1 2
The ‘for-in’ loop can be used to iterate over the array as shown below,
// for-in loop
arr = [10, 12, 31]; // array
for (a in arr){
document.write(arr[a] + " ");
}
document.write("<br>");
Continue and break statements are used inside the ‘loop’ statements.
• Continue will skip the loop and go to next iteration, if the condition is met. In the below code, 3 will not be
printed at the output.
// continue
for (i=5; i>=0; i--){
if (i==3){ // skip 3
continue;
}
document.write(i + " ");
}
document.write("<br>");
5 4 2 1 0
• ‘Break’ statement will quit the loop if the condition is met. In the below code, the for loop will be terminated
at ‘i=3’, therefore only 5 and 4 will be printed,
// break
for (i=5; i>=0; i--){
if (i==3){ // exit loop when i=3
break;
}
document.write(i + " ");
}
document.write("<br>");
5 4
4.4.8 Functions
In the below code a function ‘add2Num’ is defined which adds the two numbers and returns the result.
// function
function add2Num(num1, num2){ // function definition
return num1 + num2;
}
sum = add2Num(2, 3); // function call
document.write("2 + 3 = " + sum);
document.write("<br>");
2 + 3 = 5
One of the main usage of JavaScript is ‘event handling’. “Mouse click” and ‘pressing keys on keyboard’ are the
example of ‘events’. With the help of JavaScript, we can perform certain actions at the occurrence of the events,
as shown below.
• ‘alert’ is used to display message in the ‘pop up’ window.
function alertMessage(message){
alert(message)
}
• In Line 13, message ‘Hello’ is shown in the pop-up window on the event ‘onclick’. Note that
“JavaScript:void(0)” is used here, which does not refresh the page.
• Line 15 is same as Line 13, but “JavaScript:void(0)” is not used, therefore page will be refreshed.
• Line 17 calls the function ‘alertMessage’ to display the mesage, which is defined in the above code.
• Lines 19-25 use different events i.e. ‘onmouseover’, ‘onmouseleave’, ‘onmouseup’ and ‘onmousedown’; and
the color and the text of the string changes based on these operations.
3 <!DOCTYPE html>
4 <html>
5 <head>
6 <title>JavaScript</title>
7 </head>
8 <body>
9
10 <p id='p_name'></p>
11
12
19 <a href="JavaScript:void(0)",
(continues on next page)
54 PythonDSP
Chapter 4. JavaScript
27
31 </body>
32 </html>
4.6 Conclusion
In this tutorial, we have selected the element with certain ‘id’ using ‘getElementById’. Also, we saw the example
of ‘event handling’ Although, we can use JavaScript for these operations, but it is better to use jQuery for these
purposes, which is discussed in Chapter 5. A good approach is use to the jQuery for ‘selectors’ & ‘event handling’
and then use various features of JavaScript which are described in Section 4.4 to implement the logics.
jQuery
5.1 Introduction
5.1.1 Requirements
• First create a file ‘jQuery.html’. Next, we need three files, i.e. one CSS (Line 9) and two Javascipt (Lines
23-24), for using the jQuery in this tutorial. These files are saved inside the folder ‘asset’, which can be
downloaded from the repository. Lines 9 and 24 are the bootstrap files, whereas Line 23 is the actual
‘jQuery’ file which is required to use the jQuery.
Note: Note that, the ‘jQuery.js’ files should be placed above the ‘bootstrap.js’ file, otherwise the bootstrap-
javascipt will not work.
3 <!DOCTYPE html>
4 <html>
5 <head>
6 <title>jQuery</title>
7
12 </head>
13 <body>
14
15
16
17
18
19
20
26 </body>
27 </html>
56
Chapter 5. jQuery
• If we open this html file in the browser, the a blank page will be displayed with header ‘jQuery’.
Next add one jumbotron (Line 15-19) and few labels (Lines 21-26) in the html file as shown in Fig. 5.1.
3 <!DOCTYPE html>
4 <html>
5 <head>
6 <title>jQuery</title>
7
12 </head>
13 <body>
14
15 <div class="jumbotron">
16 <div class="col-md-offset-4 container">
17 <h1><b>jQuery</b></h1>
18 </div>
19 </div>
20
28
29
30
36 </body>
37 </html>
Now, we can add the jQuery code as shown in Lines 36-40. Please note the following part,
• $ sign is used to hit the jQuery. In the simple words, we can say that it execute the jQuery.
• The code is written between the following block. This block stops the jQuery to execute until the whole page
is loaded, i.e. jQuery will be executed after completion of the page-download.
$(function() {
// write code here
});
Note: We can use multiple blocks, if we want to separate the codes; or we can write all the jQueries within one
block.
• Lastly, the below line hides the id “l_default” after 1000 ms, i.e. ‘Default’ label will not be displayed after
1000 ms as shown in Fig. 5.2
$("#l_default").hide(1000); // hide id = 'l_default' using # operator after 300 ms
3 <!DOCTYPE html>
4 <html>
5 <head>
6 <title>jQuery</title>
7
12 </head>
13 <body>
14
15 <div class="jumbotron">
16 <div class="col-md-offset-4 container">
17 <h1><b>jQuery</b></h1>
18 </div>
19 </div>
20
58 PythonDSP
Chapter 5. jQuery
28
29
30
36 <script type="text/javascript">
37 $(function() {
38 $("#l_default").hide(1000); // hide id = 'l_default' using # operator after 300 ms
39 });
40 </script>
41 </body>
42 </html>
Note: ‘#” is used to select the ‘id’; whereas ‘.’ is sued to access the ‘class’.
• In the below code, the label ‘Danger’ is removed; and “opacity=‘0.5’ ” is set for rest of the labels. Here,
‘.label’ selects all the classes which have the name ‘label’ in it. Since all the entries have classes with name
‘label’, therefore all are selected and faded according to ‘opacity=‘0.5’, as shown in Fig. 5.3.
3 <!DOCTYPE html>
4 <html>
5 <head>
6 <title>jQuery</title>
7
12 </head>
13 <body>
14
15 <div class="jumbotron">
16 <div class="col-md-offset-4 container">
17 <h1><b>jQuery</b></h1>
18 </div>
19 </div>
20
28
29
30
36 <script type="text/javascript">
37 $(function() {
38 $("#l_default").hide(1000); // hide id = 'l_default' using # operator after 300 ms
39 $(".label-danger").hide(); // hide class = 'label' using . operator
40 $(".label").css({opacity:'0.5'}); // add opacity to all classes with name 'label'
41 });
42 </script>
43 </body>
44 </html>
It is better to write the jQuery in the separate files, therefore the codes are more manageable.
• For this, first write the code in the separate file ‘my_jquery.js’ as below,
Note: ‘html’ is used to change the content of the ‘id’ as shown in Line 5 below listing (see Fig. 5.4)
1 // asset/js/my_jquery.js
2
3 $(function() {
4 $("#l_default").hide(1000); // hide id = 'l_default' using # operator after 300 ms
5 $(".label-danger").html("Shark is dangerous"); // change content using 'html'
6 });
3 <!DOCTYPE html>
4 <html>
5 <head>
6 <title>jQuery</title>
7
60 PythonDSP
Chapter 5. jQuery
15 <div class="jumbotron">
16 <div class="col-md-offset-4 container">
17 <h1><b>jQuery</b></h1>
18 </div>
19 </div>
20
28
29
30
37 </body>
38 </html>
Below is the complete html code, which we will use in this section,
3 <!DOCTYPE html>
4 <html>
5 <head>
6 <title>jQuery</title>
7
12 </head>
13 <body>
14
15 <div class="jumbotron">
16 <div class="col-md-offset-4 container">
17 <h1><b>jQuery</b></h1>
18 </div>
19 </div>
(continues on next page)
28 <br><br>
29 <span class="btn btn-sm btn-warning" id="b_warning">Toggle Warning Label</span>
30
31 <br><br>
32 <!-- label_id is custom attribute whouse values are set according to id of above labels -->
33 <span class="btn btn-sm btn-default label-btn" label_id='l_default'>Toggle Default Label</span>
34 <span class="btn btn-sm btn-primary label-btn" label_id='l_primary'>Toggle Primary Label</span>
35 <span class="btn btn-sm btn-success label-btn" label_id='l_success'>Toggle Success Label</span>
36 <span class="btn btn-sm btn-info label-btn" label_id='l_info'>Toggle Info Label</span>
37 <span class="btn btn-sm btn-warning label-btn" label_id='l_warning'>Toggle Warning Label</span>
38 <span class="btn btn-sm btn-danger label-btn" label_id='l_danger'>Toggle Danger Label</span>
39
40
47 </body>
48 </html>
In this section, the ‘mouse click’ on the button ‘Toggle Warning Label (Line 29)’ is read; and then the label
‘warning’ is toggled.
• The method ‘on’ waits for user’s input ‘click (i.e. mouse click)’.
• On mouse-click next query i.e. ‘toggle’ is executed which hides/unhide the ‘warning label’ as shown in Fig.
5.5 - Fig. 5.6.
1 // asset/js/my_jquery.js
2
10 // toggle 'warning label' based on click on the button 'Toggle Warning Label'
11 $(function() {
12 $('#b_warning').on('click', function(){
13 $('#l_warning').toggle();
14 });
15 });
In previous section, we wrote the code which can toggle only one label. In this section, we will toggle multiple
labels by writing one-generalized-jQuery as shown in Fig. 5.7.
62 PythonDSP
Chapter 5. jQuery
• Lines 21-26 and 31-38 of Listing 5.1 are used for this section,
• Custom attributes “label_id=. . . ” are added in each button which has the value which corresponds to one
of the ‘label’s id’. e.g. ‘label_id’ for Line 33 (i.e. l_default) is same as ‘id’ at Line 21.
• Next, in the below jQuery, we saved the ‘label_id’ in a variable (see Line 21) and then the variable is used
to toggle the labels.
10 // toggle 'warning label' based on click on the button 'Toggle Warning Label'
11 $(function() {
12 $('#b_warning').on('click', function(){
13 $('#l_warning').toggle();
14 });
15 });
16
17
5.3 Selectors
In this section, we will use following HTML to understand the various combination of the selectors which are
summarized in Table 5.1,
<!DOCTYPE html>
<html>
<head>
<title>jQuery Selectors</title>
</head>
<body>
<p>jQuery Selectors</p>
<p id="bar">Paragraph with ID. <span class="foo">Span with class and inside the paragraph-with-id.</
˓→span></p>
<div>
<p my-id="my_para">Paragraph (with custom id) inside the div</p>
<p my-id="my_para2"><span>Span inside the paragraph (with custom id) inside the div</span></p>
</div>
</body>
</html>
Note:
• Press ‘ctrl+shift+k’ (or go to Tools->Web Developer->Web Console) to open the web-console in Firefox
and click on ‘console’ and type the ‘jQuery’ commands there.
• Press ‘ctrl+shift+J’ (or go to Tools->Javascript Console) in ‘Chrome’ and type the jQuery commands there.
• In this tutorial, the output of the ‘Firefox’ are shown.
HTML elements, e.g. div, p, span and table etc., can be selected by writing them with the quotation mark as
shown in below codes.
• Here, ‘$(‘p’) is the command, whereas the lines below it are the outputs.
• The outputs show the ‘selected elements’; and the total number of selected elements i.e. ‘length: 6’.
• If we put the cursor to one of the outputs, then it will highlight the corresponding elements in the html as
shown in Fig. 5.8.
64 PythonDSP
Chapter 5. jQuery
$('p')
{...}
0: <p>
1: <p class="foo">
2: <p>
3: <p id="bar">
4: <p my-id="my_para">
5: <p my-id="my_para2">
length: 6
$('div')
{...}
0: <div>
length: 1
Class can be selected using ‘.’ operator. In below code, class ‘foo’ is selected.
$(".foo")
{...}
0: <p class="foo">
1: <span class="foo">
length: 2
5.3.1.3 Select ID
ID can be selected using ‘#’ operator. In the below code, id ‘bar’ is selected.
$('#bar')
[...]
0: <p id="bar">
length: 1
We can select a element with specific class name using ‘.’ operator as shown in below code. Here element ‘span’
with class ‘foo’ is selected,
$("span.foo")
{...}
0: <span class="foo">
length: 1
Use ‘,’ to select different types of elements. In the below code, “paragraph with id ‘bar’ ” and “elements with class
‘foo’ ” are selected ,
$('p#bar, .foo')
{...}
0: <p class="foo">
1: <p id="bar">
2: <span class="foo">
length: 3
Select ‘span’ inside the ‘p’ (use space between ‘span’ and ‘p’),
$('p span')
{...}
0: <span>
1: <span class="foo">
2: <span>
length: 3
$('div>span')
{...}
length: 0
Note: There is one element inside ‘div’ but it’s path is ‘div->p->span’ therefore ‘span’ is a child of ‘p’ (not of
‘div’). But tgis ‘span’ is the descendant of ‘div’ therefore can be accessed using space,
$('div span')
{...}
0: <span>
length: 1
$('[my-id = my_para]')
{...}
0: <p my-id="my_para">
length: 1
66 PythonDSP
Chapter 5. jQuery
$('p[class = foo]')
{...}
0: <p class="foo">
length: 1
5.3.2 Filters
Various filter options (e.g. filter, first, last and has etc.) can be used after ‘:’ as shown below,
68 PythonDSP
Chapter 5. jQuery
5.4 Operations
In Listing 5.2 we saw some operations e.g. ‘hide()’ and ‘toggle()’. Table 5.2 shows some more such operations.
In Listing 5.2 we saw one example of event handling, where certain operations are performed on the event ‘mouse
click’. Table 5.2 shows a list of some more events.