jQuery
jQuery
Course covers
2006 introduced - JavaScript file that added more to what you could do
Having a experience with JavaScript and CSS will help you get started with jQuery quicker
Benefits of using jQuery
It’s small in size and loads quickly
Simple straight forward. It’s easy to learn and get started with
Its popular
● http://jquery.com/download/
● Use CDN (Content Delivery Network) https://developers.google.com/speed/libraries/
If you download make sure you place it in a directory that your can access it from.
Benefits of CDN - visitors may already have it cached within their browsers, which allows for quicker
load times.
Including the library jQuery
Link to CDN content delivery network - take a little longer to use not noticeable, but much easier to
get started with.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
Minified - whitespace is gone, all names of variables and properties as single character. Not human
readable.
The Document Object Model (DOM) is a cross-platform and language-independent convention for
representing and interacting with objects in HTML, XHTML, and XML documents. The nodes of
every document are organized in a tree structure, called the DOM tree. Objects in the DOM tree
may be addressed and manipulated by using methods on the objects. The public interface of a
DOM is specified in its application programming interface (API).
What is the DOM? Document Object Model
JavaScript allows for client-side interactivity.
Via the $ function, which is a factory method for the jQuery object. These functions, often called
commands, are chainable as they all return jQuery objects.
Via $.-prefixed functions. These are utility functions, which do not act upon the jQuery object directly.
Selectors are CSS syntax - if you are familiar with CSS selectors, jQuery selectors will be
straightforward.
jQuery
jQuery is run when the document is ready.
<script type="text/javascript">
$(document).ready(function(){
// jQuery code
});
</script>
jQuery
Same as the $(document).ready(function(){ but shorter. You can use either.
<script type="text/javascript">
$(function(){
// jQuery code
});
</script>
JQUERY
Selectors
jQuery selectors
jQuery Selectors
Once selected you probably will want to do something with the element.
Example l1.html
HTML and DOM manipulation
The DOM allows scripts to access and manipulate web documents.
text()
html()
val()
Example script1.js
Selectors SET
$(“id”).html(‘new’);
$(“.class”).html(‘new’);
$(“p”).html(‘new’);
You should be specific with get on the content. Content should be retrieved from a single element.
When you loop you generally may want to apply specific changes to each of the matching selections.
Appending of content
After
prepend
Before
Empty
Remove
});
jQuery event.preventDefault()
stops the default action of an element from happening.
event.preventDefault();
<a></a> hyperlinks…...
jQuery mouse events
hover()
dbclick()
jQuery Mouse Events
Mousedown()
mouseenter()
mousemove()
mouseleave()
mouseover();
mouseup();
jQuery Keyboard Events
keydown() keyup()
keypress()
focus()
change()
submit()
JQUERY
Traversing
jQuery Traversing
HTML elements in relation to other HTML elements.
Moving from the starting point element to other elements within the page until you reach the
desired element.
Parents
Children
siblings
jQuery Traversing family
First top element that contains others is an ancestor or parent to the elements within it.
Child is descendant of the parent, and sibling to the other elements that share the same parent.
Parent is the immediate parent whereas parents are all ancestors up to html
jQuery Traversing find
Gets all the descendants of each element
jQuery Traversing Siblings
next()
siblings()
nextAll()
jQuery Traversing Filtering
first()
last()
eq()
JQUERY
CSS
jQuery working with CSS
css(propName,value)
Add classes
Remove classes
JQUERY
EFFECTS and ANIMATIONS
jQuery effects
Simple hide() and show()
jQuery effects
Fading effects
fadeIn()
fadeOut();
fadeTo();
jQuery effects
slideDown()
slideUp()
slideToggle()
jQuery .animate()
You can perform animation
.animate()
jQuery chaining effects together
You can add more than one effect chaining methods together in jQuery
jQuery AJAX
Powerful
What is AJAX
asynchronous JavaScript and XML
Using AJAX web applications can send data to and retrieve data from a server without page reloads.
Ability to change content dynamically.
Despite the name, the use of XML is not required (JSON is often used in the AJAJ variant), and the
requests do not need to be asynchronous.
JavaScript Object Notation (JSON) is often used as an alternative format for data interchange,
although other formats such as preformatted HTML or plain text can also be used
https://en.wikipedia.org/wiki/Ajax_(programming)
AJAX
AJAX requests happen in the background making them invisible to the user.
Allowing you to access data that is not currently loaded within the page.
Uses Selectors to load the result of the AJAX call inside the selected element
Using Get to get data
$.get('php.php', function (data) {
});
});
});
https://en.wikipedia.org/wiki/Same-origin_policy
https://en.wikipedia.org/wiki/Cross-origin_resource_sharing
Thank you
Thank you for taking the course, and reading this PDF. If you have any
questions of suggestions please connect with me on Udemy.
https://www.udemy.com/user/lars51/
Laurence Svekis