Current • Edge: (Current - 1) and Current • Firefox: (Current - 1) and Current, ESR • Internet Explorer: 9+ • Safari: (Current - 1) and Current • Opera: Current • Mobile • Stock browser on Android 4.0+ • Safari on iOS 7+
console.log(’page is loaded’) ”The load event fires at the end of the document loading process. At this point, all of the objects in the document are in the DOM, and all the images, scripts, links and sub-frames have finished loading”
console.log(’page is ready’) ”The .ready() method offers a way to run JavaScript code as soon as the page's Document Object Model (DOM) becomes safe to manipulate.” => Your code can be run before downloading large images
<script src="https://code.jquery.com/jquery-3.4.1.js" integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=" crossorigin="anonymous" ></script> </head> <body> <button>Click</button> <script> jQuery(() => { jQuery('button').click(() => alert('clicked')) }); </script> </body> </html> When DOM is ready to be manipulated, add a click event to the button.
=> { if ($("button").hasClass("background-red")) { $("button").removeClass("background-red"); } else { $("button").addClass("background-red"); } }); }); </script> If button has a class, then remove the class
"hello"; </script> <script> $(() => { $("button").click(event => { alert("hello"); }); }); </script> If some other script uses the $ variable, we have a conflict Will fail because we are not using $ function from the JQuery library
$newVariable = jQuery.noConflict(); $newVariable(() => { $newVariable("button").click(event => { alert("hello"); }); }); </script> Possible to create another variable that replaces the original $ in JQuery
$( "#myId" ); • Selecting Elements by Class Name • $( ".myClass" ); • Selecting Elements by Attribute • $( "input[name='first_name']" ); • Selecting Elements by Compound CSS Selector • $( "#contents ul.people li" ); • Selecting Elements with a Comma-separated List of Selectors • $( "div.myClass, ul.people" );
contain <p> tags • $( "div.foo" ).has( "p" ); • <h1> elements that don't have a class of bar • $( "h1" ).not( ".bar" ); • unordered list items with class of current • $( "ul li" ).filter( ".current" ); • just the first unordered list item • $( "ul li" ).first(); • the sixth • $( "ul li" ).eq( 5 );
"http://www.apple.com"}); $("a").html("Apple") }); </script> </head> <body> <a href="http://www.microsoft.com">Microsoft</a> </body> You can change attributes using attr - function
document.querySelector("a") $(a).html("Microsoft") }); </script> </head> <body> <h1>Welcome</h1> <a href="http://www.apple.com">Apple</a> <a href="http://www.apple.com">Apple</a> </body> a is a DOM element Only the first one changes
$("section").find("h3") h3array[2].innerHTML = 'kk’ h3array.eq(2).html('hello world') }); </script> </head> <body> <section> <h3>Hello 1</h3> <h3>Hello 2</h3> <h3>Hello 3</h3> </section> </body> jQuery object contains a collection of Document Object Model (DOM) elements. It is array like object, but not array Demonstration of DOM Eq – function creates new jQuery object from one element with the list
the HTML contents. • .text() • Get or set the text contents; HTML will be stripped. • .attr() • Get or set the value of the provided attribute. • .width() • Get or set the width in pixels of the first element in the selection as an integer. • .height() • Get or set the height in pixels of the first element in the selection as an integer. • .position() • Get an object with position information for the first element in the selection, relative to its first positioned ancestor. This is a getter only. • .val() • Get or set the value of form elements.
$() that looks like an html, jquery creates a new element • let newP = $("<p>new element</p>") • And if you want to reference existing element, then • let existingP = $("p")
}); $( "p" ).click(function() { console.log( "You clicked a paragraph!" ); }); Then on function can be used for different events Shortcut functions available for most common events
text = event.target.value $("h1").html(text) }) }); </script> <h1>Name</h1> <input id="name" type="text" placeholder="give name" name="name" /> The event object gives you information about the event Fetching the event target’s value
use two effects by default • slide • fade • If you prefer only one effect you can • $( "p" ).slideUp("fast") • $( "p" ).slideDown("fast") • $( "p" ).fadeIn("fast") • $( "p" ).fadeOut("fast")
trigger action when animation completes • Example • $( "p" ).fadeToggle(800, () => alert('done’)) • Keyword this refers to the element that did the animation • $( "p" ).fadeToggle(800, () => $(this).addClass('change'))