A Paragraph
` elements | | `$("#id")` | Select elements by id | | `$(".class")` | Select elements by class | | `$(object)` | Converts object to jQuery object (`this`, `document`, ...) | .center[[W3Schools jQuery Selectors](https://www.w3schools.com/jquery/jquery_selectors.asp)] --- # jQuery: Handling events HTML allows event handler attributes, with JavaScript code, to be added to HTML elements. ```html The time is? ``` The time is? Events can be defined inside in the JavaScript files using jQuery, instead of the HTML: ```html The time is? ``` ``` $(document).ready(function() { $("#date-button").click(function() { $(this).text(Date()); }); }); ``` --- # jQuery: Handling events .center[[W3Schools jQuery Events](https://www.w3schools.com/jquery/jquery_events.asp)] | Mouse Events | Keyboard Events | Form Events | Document/Window Events | | - | - | - | - | | click | keypress | submit | load | | dblclick | keydown | change | resize | | mouseenter | keyup | focus | scroll | | mouseleave | | blur | unload | --- # jQuery: Hide/Show jQuery provides the following methods to hide and show elements: - `hide()`: Hides the selected elements. - `show()`: Shows the selected elements. - `toggle()`: Toggles between hide and show. - `fadeIn()`, `fadeOut()`, `faddeToggle()`, `fadeTo()`: Fading elements in and out of visibility. - `slideDown()`, `slideUp()`, `slideToggle()`: Sliding elements up and down. (Better use CSS) .center[[W3Schools jQuery Hide/Show](https://www.w3schools.com/jquery/jquery_hide_show.asp)] .center[[W3Schools jQuery Fade](https://www.w3schools.com/jquery/jquery_fade.asp)] .center[[W3Schools jQuery Slide](https://www.w3schools.com/jquery/jquery_slide.asp)] --- # jQuery: Get Content jQuery provides the following methods to get or set the content from elements: - `text()`: Sets or gets the inner text of the selected elements. - `html()`: Sets or gets the content of the selected elements (including HTML tags). - `val()`: Sets or gets the value of form fields. - `find(selector)`: Searches a child elements using a selector. .center[[W3Schools jQuery Get](https://www.w3schools.com/jquery/jquery_dom_get.asp)] .center[[W3Schools jQuery Set](https://www.w3schools.com/jquery/jquery_dom_set.asp)] --- # jQuery: Get Content Examples ```html
The length of the name is: 4
$("div").find(".name").text(name); // Searches elements with class "name" inside all divs // Sets the innerHTML to name variable ``` --- # jQuery: Add/Remove elements jQuery provides the following methods to add or remove HTML elemets. - `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 - `remove()` - Removes the selected element (and its child elements) - `empty()` - Removes the child elements from the selected element .center[[W3Schools jQuery Add](https://www.w3schools.com/jquery/jquery_dom_add.asp)] .center[[W3Schools jQuery Remove](https://www.w3schools.com/jquery/jquery_dom_remove.asp)] --- # jQuery: Add/Remove classes jQuery provides the following methods to add or remove classes from HTML elemets. - `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 - `hasClass()` - Returns `true` or `false` if the element has a class or not. - `css()` - Sets or returns the style attribute .center[[W3Schools jQuery Manipulationg CSS](https://www.w3schools.com/jquery/jquery_css_classes.asp)] .center[[W3Schools jQuery CSS](https://www.w3schools.com/jquery/jquery_css.asp)]