W9 JQuery - Part 1 - Module PDF
W9 JQuery - Part 1 - Module PDF
W9 JQuery - Part 1 - Module PDF
Learning Objectives
Introduction
jQuery is a javascript library that makes web scripting easier for us.
Before, most website builders use HTML and JavaScript to build websites. They uses HTML for
web content just like today and for web design since there were no CSS yet. Also with the help of
JavaScript scripting, they were able to manipulate, interact and make use of web elements especially for
events/triggers.
Download JQuery
https://jquery.com/download/
How to jQuery
In order to use jQuery, you should do any of the following:
a. Download jQuery into your application folder and use it in the view:
It is almost identical to a.) but rather than downloading the jQuery js file, it is linked to a jQuery
js file on a hosted server (or jQuery site) instead.
After linking the jQuery resource js file, create an internal or external script with a
$(document).ready() function in it. This function will be called if the browser contents has properly
You can put this code right after the jQuery script linking(<script src…) line.
If the page is ready, it will show a modal with a message “jQuery is ready!” like this:
$(document).ready(function(){
//code here
});
With jQuery, you can call or target HTML elements. If you know targeting elements in CSS, you
can also target elements in jQuery!
$( element )
Where inside the parenthesis you can simply call the element tag,
Example:
$( p ) //which targets all paragraph elements
$( div ) //targets all div elements
$( input ) //targets the input elements
element ID,
Example:
$( “#header” ) //targets all element who has an ID of header
And/or class
Example:
$( “.navigation-item” ) //targets elements with the class: navigation-item
When selecting ID, we have to put the element string inside quotes and include a (#) number symbol
prefix. On the other hand, classes uses (.) dot symbol prefix.
jQuery Methods
So what’s next after learning to call or target elements? We have jQuery Methods. These are functions
that manipulate the selected element. Below are some:
.val()
It is used to get or set values from and to an input element particularly textboxes.
Example
var x = $( “#txtname” ).val() ; //gets the value of the id “txtname” and assigns it to a variable
.text()
It is used to get or set values from and to a non-input element like divs.
Example
var y = $( “.header” ).text(); //gets the value of the class “header” and assigns it to a variable
$( “.header” ).text( “jQuery Tutorial” ); //sets the value to “jQuery Tutorial”
.hide()
It is used to hide selected element(s)
Example:
$(“#some-item”).hide(); //hides the element with the id “some-item”
.show()
It is used to show selected element(s)
Example:
$(“#hidden-item”).show(); //shows the element with the id “hidden-item”
.toggle()
It is used to toggle, switch or alternate the selected item’s visibility: show and hide
Example:
$(“#some-item”).toggle(); //toggles the element with the id “some-item”
.fadeIn(interval)
It is used to produce a fading in effect within a given time on a selected element
Example:
$(“#some-item”).fadeIn(3000); //fades-in the item in 3 sec (3000ms)
.fadeOut(interval)
It is used to produce a fading out effect within a given time on a selected element
Example:
$(“#some-item”).fadeOut(3000); //fades-out the item in 3 sec (3000ms)
.slideUp(interval)
It is used creates a sliding up transition within a given time on a selected element
Example:
$(“#some-item”). slideUp (3000); //slide the item up in 3 sec (3000ms)
.slideDown(interval)
It is used creates a sliding down transition within a given time on a selected element
Example:
$(“#some-item”). slideDown (3000); // slide the item down in 3 sec (3000ms)
.slideLeft(interval)
It is used creates a sliding to the left transition within a given time on a selected element
Example:
$(“#some-item”). slideLeft (3000); // slide the item to the left in 3 sec (3000ms)
.slideRight(interval)
It is used creates a sliding to the right transition within a given time on a selected element
Example:
$(“#some-item”). slideRight (3000); // slide the item to the right in 3 sec (3000ms)
Before a specific jQuery action execute, we call the Event Handlers to trigger the action. After
targeting elements, we instantly add the event handler that will be called to execute the instructions
within the code block. Examples of event handlers are click, dblclick, mouseover and many more. (see
$( “#btnsubmit” ).click(function(){
});
This will show a message “The submit button is clicked!” on a pop-up modal upon a single click.
$( “#btnsubmit” ).dblclick(function(){
});
This will show a message “The submit button is clicked twice!” on a pop-up modal. This will be triggered
by a double mouse click.
$( “#btnsubmit” ).mouseover(function(){