3 - Javascript JQuery-MTD
3 - Javascript JQuery-MTD
• Variable declaration:
• var variableName = value;
• variableName = value;
Variables
7
// single-line comment
/* multi-line comment */
JS
if (condition) {
statements;
} else if (condition) {
statements;
} else {
statements;
}
JS
identical structure to Java's if/else statement
Javascript allows almost anything as a condition
for loop
18
for (var i = 1; i < 7; i++) {
document.write("<h"+i+">Header "+i+"</h"+i+">");
}
// prints six HTML headers
JS
var s1 = "hello";
var s2 = "";
for (var i = 0; i < s.length; i++) {
s2 += s1.charAt(i) + s1.charAt(i);
}
// s2 stores "hheelllloo" JS
while loops
19
while (condition) {
statements;
} JS
do {
statements;
} while (condition);
JS
Object
20
Object types
Internal (built-in): String, Array, Math, Date
External: All HTML elements in HTML document,
Window, user-defined object
DOM element objects
21
Math object
22
Document
All HTML tags in HTML document is stored and can be
accessed with this object
History
Location
Pop-up boxes (Alert, Confirm, Prompt)
Popup boxes
24
alert("message"); // message
confirm("message"); // returns true or false
prompt("message"); // returns user input string
JS
Event-driven programming
25
function name() {
statement ;
statement ;
...
statement ;
} JS
//example
function mySalute() {
alert("Hello!");
alert("How are you?");
} JS
//function declaration
function add(bil1, bil2) {
var result= bil1 + bil2;
return result;
} JS
//function execution
mySalute(); //no return value
var val = add(3,7); //with return value, assign variable
document.write(add(3,7)); /* with return value, as input
for another function */
JS
$(document).ready(function(){
});
$(function(){
});
jQuery Selectors
Syntax Description
$("*") Selects all elements
$("ul li:first") Selects the first <li> element of the first <ul>
$("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"
$(document).ready(function(){
$("p").click(function(){
// action goes here!!
});
});
jQuery - Get Content and Attributes
• Three simple, but useful, jQuery methods for DOM manipulation are:
• text() - Sets or returns the text content of selected elements
• html() - Sets or returns the content of selected elements (including HTML
markup)
• val() - Sets or returns the value of form fields
• $("#btn2").click(function(){
alert("HTML: " + $("#test").html());
});
• The jQuery attr() method is used to get attribute values.
• $("button").click(function(){
alert($("#w3s").attr("href"));
});
• The jQuery data() method is used to get attribute values.
• $("button").click(function(){
alert($("#w3s").data(“option"));
});
jQuery - Set Content and Attributes
• We will use the same three methods from the previous page
to set content:
• text() - Sets or returns the text content of selected elements
• html() - Sets or returns the content of selected elements (including
HTML markup)
• val() - Sets or returns the value of form fields
• $("#btn2").click(function(){
$("#test2").html("<b>Hello world!</b>");
});
• The jQuery attr() method is also used to set/change attribute
values.
• $("button").click(function(){
$("#w3s").attr({
"href" : "https://www.w3schools.com/jquery/",
"title" : "W3Schools jQuery Tutorial"
});
});
jQuery - Add Elements
• We will look at four jQuery methods that are used to add new content:
• append() - Inserts content at the end of the selected elements
• $("table").append("<tr><td>add row append table</td></tr>");
• prepend() - Inserts content at the beginning of the selected elements
• $("table").append("<tr><td>add row prepend table</td></tr>");
• after() - Inserts content after the selected elements
• $("p").after("<span>Some text after</span>");
• before() - Inserts content before the selected elements
• $("p").after("<span>Some text before</span>");
jQuery - Remove Elements
• remove() - Removes the selected element (and its
child elements)
• $("#div1").remove();
• empty() - Removes the child elements from the
selected element
• $("#div1").empty();
• The following example removes all <p> elements with
class="test" and class="demo":
• $("p").remove(".test, .demo");
jQuery - Get and Set CSS Classes
• Query has several methods for CSS manipulation. We will
look at the following methods:
• addClass() - Adds one or more classes to the selected
elements
• $("h1, h2, p").addClass("blue");
• removeClass() - Removes one or more classes from the
selected elements
• $("h1, h2, p").removeClass("blue");
• toggleClass() - Toggles between adding/removing classes
from the selected elements
• $("h1, h2, p").toggleClass("blue");
• css() - Sets or returns the style attribute
• $("p").css("background-color", "yellow");
jQuery Dimension Methods
• jQuery has several important methods for working
with dimensions:
• width()
• height()
• innerWidth()
• innerHeight()
• outerWidth()
• outerHeight()
• $("button").click(function(){
var txt = "";
txt += "Width: " + $("#div1").width();
txt += "Height: " + $("#div1").height();
$("#div1").html(txt);
});
Looping over the DOM
<ul>
<li>foo</li>
<li>bar</li>
</ul>
$( "li" ).each(function() {
$( this ).addClass( "foo" );
});
Any question?
Exercise
• Create a function that calculates only the odd values in
a sequence of numbers, given the min and max
sequence value as inputs. Example:
• min = 1, max = 8, result = 16 (1+3+5+7)
• min = 4, max = 15, result = 60 (5+7+9+11+13+15)
• Open your previous CSS exercise file, add two buttons
that call a function when clicked:
• First button: change the alignment of the text to center or
right
• Second button: change the background color of one of the
box. When button is clicked again, the background color is
back to original color
References
• https://www.w3schools.com/js/default.asp
• http
://www.webstepbook.com/supplements-2ed/slides/le
ctureXX-jquery.shtml
• https://api.jquery.com
• https://www.w3schools.com/jquery/
THANK YOU