WOULDN'T IT BE GREAT IF SOMEONE WROTE A LIBRARY OF PRE-MADE FUNCTIONS THAT WAS EASY TO USE??
//Add and remove a class
function addClass( classname, element ) {
var cn = element.className;
//test for existance
if( cn.indexOf( classname ) != -1 ) {
return;
}
//add a space if the element
//already has class
if( cn != '' ) {
classname = ' '+classname;
}
element.className = cn+classname;
}
function removeClass( classname, element ) {
var cn = element.className;
var rxp = new RegExp( "s?b"+classname
+"b", "g" );
cn = cn.replace( rxp, '' );
element.className = cn;
}
var myElement = document
.getElementByID('myEl');
addClass('active', myElement);
removeClass('idle', myElement);
//with jQuery
$('#myEl')
.addClass('active')
.removeClass('idle');
jQuery adds 87Kb (compressed) to the page, meaning a 87 ms delay in loading plus 1/6 blocking
You can:
<script src="https://cdnjs.cloudflare.com
/ajax/libs/jquery/3.3.1/jquery.js"></script>
<script src="./scripts/main.js"></script>
Make sure to use the right version of jQuery:
Make sure you put your script afterwards!!
$(function(){
//Your code goes here...
});
$("p");
$("ul li");
Phase 1: Select Element(s)
$(".myClass");
$(".myClass p");
$(".myClass .myChildClass");
$("#myDiv");
Phase 1: Select Element(s)
There are lots of combinations
$("#dropdown-menu").slideUp();
$(".my-icons").fadeOut();
Phase 2: Manipulation
$("#dropdown-menu")
.slideUp().fadeOut()
.parents(".myParent")
.slideUp().end().fadeIn();
Phase 2: Manipulation
$("#myDiv").show();
$("#myDiv").css({
color: "red"
});
$("#myDiv").slideUp();
Phase 1: Select Element(s)
RE-QUERY
When you call the jQuery main function on an element it is EXPENSIVE. It takes ALL the measurements required for ALL the functions. If you do that repeatedly (as above) then it slows your code down.
var $myDiv = $("#myDiv");
$myDiv.show().css({
color: "red"
}).slideUp();
Phase 1: Select Element(s)
RE-QUERY - solved
This is called caching. We measure once and save as a variable. By convention (to remind us we've measured it once) we put a $ before the variable name.
$("#myContainer")
.parents(".main-container")
.children("ul")
.find("li");
JQuery measures the first element, then you can use functions like the above to move around the DOM and find things, filtering elements out as it goes.
To go back to the before you used the last filter use the .end() method.
//To show and hide an element(s) we can do
var $els = $('.myEls');
//separately
$els.show();
$els.hide();
$els.toggle();
$els.fadeIn();
$els.fadeOut();
$els.fadeToggle();
$els.slideDown();
$els.slideUp();
$els.slideToggle();
//To show and hide an element(s) we can do
var $els = $('.myEls');
//separately
$els.addClass('on');
$els.removeClass('on');
$els.toggleClass('on');
var $redLightControl = $('#red-light-control');
$redLightControl.on('click.onOffToggle', function($e){
var $redLights = $('.light.stop');
$redLights.toggleClass('on');
return false;
});
Notes:
var $redLightControl = $('#red-light-control');
$redLightControl.trigger('click.namespace', extraData);
Notes:
Pick up the element and call the function...
// jQuery plugins are extra peices of code that use jQuery to accomplish a task.
// To keep things in order jQuery generally insisted that they follow the same pattern
//STEP 1 - Find the DOM node you want to fire it on
//STEP 2 - call that function! So...
$('body').masonry();
$('ul.gallery').lightBox({
pagination: true,
easing: 'ease-out'
});
// Sites will often have a GitHub repository with instructions, or a dedicated website,
// if in doubt...