JQUERY Dr Mohd Soperi Mohd Zahid Semester 2 2015/16.

Slides:



Advertisements
Similar presentations
5.1 JavaScript Execution Environment
Advertisements

JavaScript and the DOM Les Carr COMP3001 Les Carr COMP3001.
JavaScript Part 6. Calling JavaScript functions on an event JavaScript doesn’t have a main function like other programming languages but we can imitate.
JavaScript Forms Form Validation Cookies. What JavaScript can do  Control document appearance and content  Control the browser  Interact with user.
JavaScript Forms Form Validation Cookies CGI Programs.
Computer Science 103 Chapter 4 Advanced JavaScript.
JavaScript 101 Lesson 5: Introduction to Events. Lesson Topics Event driven programming Events and event handlers The onClick event handler for hyperlinks.
Philly.NET Hands-On jQuery + Plug-ins Bill Wolff, Rob Keiser.
CHAPTER 12 UNLEASHING JAVASCRIPT USING JQUERY. LEARNING OBJECTIVES How to download the JQuery library. How to integrate the JQuery library into an HTML.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript forms.
JS: DOM Form Form Object Form Object –The Form object represents an HTML form. –For each instance of a tag in an HTML document, a Form object is created.
HTML Forms/Events (Instructor Lesson) The Event model HTML Forms Custom Events 1.
CNIT 133 Interactive Web Pags – JavaScript and AJAX Event and Event Handling.
HTML DOM.  The HTML DOM defines a standard way for accessing and manipulating HTML documents.  The DOM presents an HTML document as a tree- structure.
JS: Document Object Model (DOM)
Chapter 19: Adding JavaScript
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 JavaScript and HTML Documents.
JavaScript Informatics for economists I. Introduction Programming language used in web pages. Simple and easy to use – written in HTML document. Client.
UFCEWT-20-3 Advanced Topics in Web Development Lecture 4 : JavaScript, Browsers & the HTML DOM-API.
DOM and JavaScript Aryo Pinandito.
CSS Class 7 Add JavaScript to your page Add event handlers Validate a form Open a new window Hide and show elements Swap images Debug JavaScript.
Lesson13. JavaScript JavaScript is an interpreted language, designed to function within a web browser. It can also be used on the server.
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
INTRODUCTION TO HTML5 Using jQuery with HTML5. Introducing jQuery  Although it is not a part of any W3C or WHATWG specification, jQuery performs an important.
Javascript II DOM & JSON. In an effort to create increasingly interactive experiences on the web, programmers wanted access to the functionality of browsers.
CO1552 Web Application Development HTML Forms, Events and an introduction to JavaScript.
Client-Side Scripting JavaScript.  produced by Netscape for use within HTML Web pages.  built into all the major modern browsers. properties  lightweight,
JavaScript, jQuery, and Mashups Incorporating JavaScript, jQuery, and other Mashups into existing pages.
Lecture 10 JavaScript: DOM and Dynamic HTML Boriana Koleva Room: C54
Event JavaScript's interaction with HTML is handled through events that occur when the user or browser manipulates a page. When the page loads, that is.
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.
JQuery Introduction © Copyright 2014, Fred McClurg All Rights Reserved.
Event-Driven Programming Chapter Page Section: Section or division of an HTML page Generic block element Example: What's the difference between.
1 Javascript DOM Peter Atkinson. 2 Objectives Understand the nature and structure of the DOM Add and remove content from the page Access and change element.
JQuery JavaScript is a powerful language but it is not always easy to work with. jQuery is a JavaScript library that helps with: – HTML document traversal.
Jaana Holvikivi 1 Introduction to Javascript Jaana Holvikivi Metropolia.
1 CSC160 Chapter 7: Events and Event Handlers. 2 Outline Event and event handlers onClick event handler onMouseOver event handler onMouseOut event handler.
Event Handling. Objectives Using event handlers Simulating events Using event-related methods.
INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.
Web Programming Java Script & jQuery Web Programming.
CIS 3.5 Lecture 2.3 "Introduction to JavaScript".
Unit 13 –JQuery Basics Instructor: Brent Presley.
DOM (Document Object Model) - Parsing and Reading HTML and XML -
Dr. Ahmet Cengizhan Dirican BIL 374 Internet Technologies 5. JavaScript and HTML Documents.
5 th and 4 th ed: some from chapters 9, 12, 13 SY306 Web and Databases for Cyber Operations Slide Set #8: Dynamic HTML.
School of Computing and Information Systems CS 371 Web Application Programming JavaScript - DOM Modifying the Page from within.
Understanding JavaScript and Coding Essentials Lesson 8.
Document Object Model Nasrullah. DOM When a page is loaded,browser creates a Document Object Model of the Page.
JavaScript II ECT 270 Robin Burke. Outline Functions Events and event handling Form validation.
JavaScript Event Handlers. Introduction An event handler executes a segment of a code based on certain events occurring within the application, such as.
LESSON : EVENTS AND FORM VALIDATION -JAVASCRIPT. EVENTS CLICK.
JS: Document Object Model (DOM) DOM stands for Document Object Model, and allows programmers generic access to: DOM stands for Document Object Model, and.
Introduction to JQuery COGS 187A – Fall JQuery jQuery is a JavaScript library, and allows us to manipulate HTML and CSS after the page has been.
Check Boxes. 2 Check boxes  Image from:  HTML:  Each box gets it's own.
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
CIS 228 The Internet 12/6/11 Forms and Validation.
Introduction to JavaScript Events Instructor: Sergey Goldman 1.
JavaScript 1021 Nancy Leiby December 11, JavaScript 1022 JavaScript Notes Debugging JavaScript in Netscape … put ‘javascript:’ in the URL text box.
SE-2840 Dr. Mark L. Hornick 1 Dynamic HTML Handling events from DOM objects.
Third lecture Event 27/2/2016 JavaScript Tutorial.
THE DOM.
Week 3: Introduction to Javascript
Week 4: Introduction to Javascript
Introduction to JavaScript Events
Introduction to Web programming
CHAPTER 7 JavaScripts & HTML Documents
E-commerce Applications Development
JavaScript and Forms Kevin Harville.
Web Programming and Design
Web Programming and Design
Presentation transcript:

JQUERY Dr Mohd Soperi Mohd Zahid Semester /16

Introduction to jQuery To use jQuery, obtain it from and integrate it into a webpagehttp:// Two versions are available for download: production version and development version Use the minified production version unless you are interested at the internals of jQuery Another option is to reference it from a CDN site instead of downloading

Including jQuery in a web page Same method as using external javaScript file Latest version now may not be 1.7.2, check the web site

jQuery syntax When jQuery library is included in a page, a function called jquery() is added Fortunately, calls to jquery() to access jQuery library can be made by just referring to its shortcut, that is $()

Using $(document).ready() Document Ready $(document).ready(function() { alert(‘hi’); });

Using selectors Given Link Accessing with normal JavaScript: getElementById(“linkOne”) Accessing with jQuery: $(“#linkOne”) Given Accessing with jQuery: $(“.specialClass”) Other examples: $(‘div’), $(‘a’), $(‘div a’), $(“p:nth-child(3)”), $(“p:first”), $(“p:last”), etc $(“p”)[1] selects the second element $(“p:eq(3)”) selects the third paragraph

Activity 11

Activity 12

Inserting elements Given Here is a div, it’s quite nice $(“myDiv”).before(“ This is a new div ”); inserts another element before the above $(“myDiv”).after(“ This is another new div ”); inserts another element after the above Appending an element $(document).ready(function() { $(‘body’).append(‘ Adding an element ’); });

Document Object Model (DOM) Provides a way to access and alter the contents of HTML documents Represents HTML documents in a tree-like structure

Working with DOM Retrieving elements Setting elements and items related to elements Removing or deleting elements Adding nodes to an existing parent

Methods and Properties getElementById() getElementByTagName() … many more innerHTML childNodes

Activity 13 Get By id function checkref() { var a1 = document.getElementById("w3link"); alert(a1.href); } Here's some text. Here's more text. Link to the W3

Activity 14 (innerHTML property) Get By Id function changetext() { var p1 = document.getElementById("sometext"); alert(p1.innerHTML); p1.innerHTML = "Changed Text"; } Here's some text. Here's more text. Link to the W3

Changing elements and attributes Get By Id $(document).ready(function() { function changetext() { alert($("#sometext").html()); $("#sometext").html("Changed Text"); } changetext(); }); Here's some text. Here's more text. Link to the W3

Retrieving by tag name Tag Name function changecolors() { var a1 = document.getElementByTagName("td"); var a1length = a1.length; for (var i = 0; i < a1length; i++) { a1[i].style.background = "#aaabba";} } Left column Right column Click to Change Colors

Using jQuery Tag Name function changecolors() { $("td").each(function() { $(this).css("background-color", "#aaabba"); }); } Left column Right column Click to Change Colors

Objects having groups of elements document.anchors document.forms document.images document.links

childNodes, parentNode, … A property containing a group of nodes comprising the children of a given element To retrieve the first child of a element with id mydiv, and store it in a variable var childOne = document.getElementById(“mydiv”).childNodes[0] To retrieve the parent of a child, the parentNode property can be used nextSibling, previousSibling, firstChild, lastChild jQuery has the children() function which returns the children of the matched element; parent(), prev() and next() functions are available

Changing Attribute of element Set Attribute Steve Suehring's Web Site var a1 = document.getElementById("braingialink"); alert(a1.getAttribute("href")); a1.setAttribute("href", " alert(a1.getAttribute("href"));

Create and Remove Element Create Element var newelement = document.createElement("p"); newelement.setAttribute("id", "newelement"); document.body.appendChild(newelement); newelement.appendChild(document.createTextNode("Hello World")); alert(newelement.getAttribute("id")); for (var i = 0; i < 3; i++) { var element = document.createElement("p"); element.setAttribute("id", "element" + i); document.body.appendChild(element); element.appendChild(document.createTextNode("Hello World, I'm Element " + i + ".")); } var removeelm = document.getElementById("element1"); document.body.removeChild(removeelm);

Events HTML tags raised in response to various user actions or state changes onblur() – the element lost focus, no longer selected onchange() – user typed into a text field, element changes onclick() – the mouse clicked an element onfocus() – the element got focus onload() – the element is loaded (document or image) onmousedown() – mouse pressed while elem has focus onmouseover() – mouse is over an element onsubmit() – the form is submitted Many more…

Event handling First event is registered by associating a function with the event When a registered event gets triggered, the event’s function is called

Activity 22 Timer Hello function sendAlert() { alert("Hello"); } function startTimer() { var timerID = window.setTimeout(sendAlert, 3000); } $(window).load(function() { startTimer(); });

jQuery and image hover example $(window).load(function() { $('#home').hover(function() { $("#home").attr("src", "helping.jpg"); }, function() { $("#home").attr("src", “butterfly.jpg”); });

jQuery and Slideshow Slideshow var images = ['helping.jpg', 'butterfly.jpg', 'hadith.gif', 'islam.jpg']; function nextImage() { var index = $('img[name=slideimage]').attr("id"); if (index == images.length - 1) { index = 0; } else { index++; } $('img[name=slideimage]').attr("src", images[index]); $('img[name=slideimage]').attr("id", index); } function prevImage() { var index = $('img[name=slideimage]').attr("id"); if (index == 0) { index = images.length - 1; } else { index--; } $('img[name=slideimage]').attr("src", images[index]); $('img[name=slideimage]').attr("id", index); }

jQuery and Slideshow (cont.) $(window).load(function() { $("#prevbtn").on("click", function() { prevImage(); }); $("#nextbtn").on("click", function() { nextImage(); });

Form Validation with jQuery A Basic Example $(document).ready(function() { $("#myForm").submit(function(eventObj) { if ($("#textbox1").val() == "") { alert("Name is required."); eventObj.preventDefault(); return false; } else { alert("Hello " + $("#textbox1").val()); return true; } }); A Basic Form Example Name (Required) :

jQuery and Select Box Select boxes Select A Constellation: Aquila Centarus Canis Major Canis Minor Corona Borealis Crux Cygnus Gemini Lyra Orion Taurus Ursa Major Ursa Minor

jQuery and Select box (cont) $(document).ready(function() { $("#starselect").change(function(eventObj) { alert($("#starselect").val()); });

jQuery changes selected option Pizza function flip(pizzatype) { if (pizzatype.value == "Veggie Special") { $("#topping").val("veggies"); } else if (pizzatype.value == "Meat Special") { $("topping").val("meat"); } else if (pizzatype.value == "Hawaiian") { $("topping").val("pineapple"); }

jQuery changes selected option Main Topping: Cheese Veggies Meat Olive & Pineapples $(document).ready(function() { $('input[name^=special]').each(function() { $(this).on("click", function() { flip(this); });

jQuery with Select box & Radio button Pizza function prepza() { $("#orderheading").text("This pizza will have:"); $("input[name=toppingcheck]:checked").each(function() { $("#orderheading").append(' ' + $(this).val() + ' '); }); $("#orderheading").append(' ' + $("input[name=crust]:checked").val() + ' Crust '); return false; }

jQuery with Select box & Radio button Toppings Crust <input type="checkbox" id="topping1" value="Sausage" name="toppingcheck">Sausage <input type="radio" name="crust" value="Regular" checked="checked" id="radio1">Regular <input type="checkbox" id="topping2" value="Pepperoni" name="toppingcheck">Pepperoni <input type="radio" name="crust" value="Deep Dish" id="radio2">Deep Dish <input type="checkbox" id="topping3" value="Olive" name="toppingcheck">Olive <input type="radio" name="crust" value="Thin" id="radio3">Thin <input type="checkbox" id="topping4" value="Green Peppers" name="toppingcheck">Green Peppers

jQuery with Select box & Radio button <input type="checkbox" id="topping5" value="Mushrooms" name="toppingcheck">Mushrooms <input type="checkbox" id="topping6" value="Onions" name="toppingcheck">Onions <input type="checkbox" id="topping7" value="Pineapple" name="toppingcheck">Pineapple $(document).ready(function() { $("#prepBtn").on("click", function() { return prepza(this); });

jQuery with input checking Catalog Form Order quality books From My Bookstore Description: Gain your knowledge about Islam and other topics, all are available at My Bookstore. Quantities are extremely limited. Make your order now! Price: $ per box Quantity to order (Limit 3 per purchase): $(document).ready(function() { $("#catalogform").on("submit", function(event) { if ($("input[name=quantity]").val() > 3) { $("input[name=quantity]").after(' Limit 3 per \ purchase '); $("#errorSpan").css("background-color", "grey"); event.preventDefault(); return false; } else { return true; } });