IP - Chapter 4
IP - Chapter 4
IP - Chapter 4
This helps to prevent any jQuery code from running before the
document is finished loading (is ready).
Alternatively:
jQuery Selectors
jQuery selectors are used to "find" (or select) HTML elements based
on their id, classes, types ….
It's based on the existing CSS Selectors, and in addition, it has some
own custom selectors.
All selectors in jQuery start with the dollar sign and parentheses: $()
The element selector [$(‘p’)], #id selector [$(‘#test’)], .class selector [$
(‘.test’)] …
More: jQuery Selectors
$("*"): Selects all elements
$"p.intro"): Selects all <p> elements with class="intro"
$("ul li:first"): Selects the first <li> element of the first <ul>
$("ul li:first-child"): Selects the first <li> element of every <ul>
$("[href]"): Selects all elements with an href attribute
$("a[target='_blank']"): Selects all <a> elements with a target attribute value
equal to "_blank"
$(":button"): Selects all <button> elements and <input> elements of
type="button"
jQuery Event Methods
jQuery is tailor-made to respond to events in an HTML page
jQuery Syntax For Event Methods
To assign a click event to all paragraphs on a page, you can do this:
$("p").click();
The next step is to define what should happen when the event fires.
You must pass a function to the event:
Or you can use the “on” method:
jQuery Event Methods
$(document).ready() :allows you to execute a function when the
document is fully loaded
click(): executed when the user clicks on the HTML element.
dblclick(): executed when the user double-clicks on the HTML
element.
mouseenter(), mouseleave(), mousedown(), mouseup(), hover(),
focus(), blur(), on()
jQuery Effects: Hide/ Show
Syntax: $(selector).hide(speed,callback);
$(selector).show(speed,callback);
$(selector).toggle(speed,callback);
The optional speed parameter specifies the speed of the
hiding/showing, and can take the following values: "slow", "fast", or
milliseconds.
The optional callback parameter is a function to be executed after
the hide() , show() or toggle() method completes.
jQuery Effects: Fade
With jQuery you can fade an element in and out of visibility.
jQuery has the following fade methods:
◦ fadeIn(speed,callback) is used to fade in a hidden element.
◦ fadeOut(speed,callback) is used to fade out a visible element
◦ fadeToggle(speed,callback) toggles between the fadeIn() and
fadeOut() methods
◦ fadeTo(speed,opacity,callback) allows fading to a given opacity
(value between 0 and 1)
jQuery Effects: Slide
With jQuery you can create a sliding effect on elements.
jQuery has the following slide methods:
◦ slideDown(speed, callback) is used to slide down an element
◦ slideUp(speed, callback) is used to slide up an element
◦ slideToggle() toggles between slide up and slide down.
jQuery DOM Manipulation
jQuery offers the following methods for DOM manipulation:
◦ 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
◦ attr() – Sets or returns attribute values
jQuery Add Elements
With jQuery, it is easy to add new elements/content.
Four jQuery methods that are used to add new content:
append() - Inserts content at the end of the selected elements
prepend() - Inserts content at the beginning of the selected
elements
after() - Inserts content after the selected elements
before() - Inserts content before the selected elements
jQuery Remove Elements
To remove elements and content, there are mainly two jQuery
methods:
remove() - Removes the selected element (and its child elements)
◦ also accepts one parameter, which allows you to filter the elements to be
removed.
◦ The parameter can be any of the jQuery selector syntaxes.
empty() - Removes the child elements from the selected element
jQuery CSS
jQuery has several methods for CSS manipulation. We will look at the
following methods:
addClass() - Adds one or more classes to the selected elements
removeClass() - Removes one or more classes from the selected elements
toggleClass() - Toggles between adding/removing classes from the selected
elements
css() - Sets or returns one or more style properties for the selected
elements
Syntax: css("propertyname"); & css("propertyname","value");
Reading Assignment
JavaScript Math function
jQuery Callback functions
jQuery method chaining
jQuery Animate
jQuery AJAX