Classes (p131)

For the following HTML:
<span>this line is important </span>


.addClass() -- adds a specified class to an element

   $('span').addClass('highlight'); 

   // <span class="highlight">this line is important </span>


.removeClass() -- removes a specified class

   $('span').removeClass('highlight'); 

   // <span>this line is important </span>


.toggleClass -- toggle (add if it doesn't exist and remove if it exists) a class [jq_02_toggleClass.html]
<!DOCTYPE html>
<html>
<head>
<title>jQuery</title>
<style>
.highlight {
    background: yellow;
  }
</style>
<script src="http://code.jquery.com/jquery-3.2.1.js"></script>
</head>
<body>

<button>highlight</button>


Hello. <span>This line is important</span>. But not this.

<script>
$('button').click(function(){               // don't worry about this line
    $('span').toggleClass('highlight');
});                                         // don't worry about this line
</script>
</body>
</html>
Notes

Reading and Changing CSS Properties (p134)

.css() -- to read or change the CSS values
// getting
var color = $('body').css( "background-color" ); // get the background color for <body>

// setting
$('p').css('font-size','200%');           // set the font-size
$('p').css('background-color', 'yellow');  // set the background color 


// this is more efficient (chaining)
$('p').css('font-size','200%').css('background-color', 'yellow'); 


[jq_02_css.html]
<!DOCTYPE html>
<html>
<head>
<title>jQuery</title>
<script src="http://code.jquery.com/jquery-3.2.1.js"></script>
</head>
<body>
<button>larger!</button>

<p> paragraph 1 </p>
<p> paragraph 2 </p>
<p> paragraph 3 </p>

<script>
$('button').click(function(){        

    var baseFont = $('body').css('font-size');
    baseFont = parseInt(baseFont); //parseInt() is a javascript function. turns a string into an integer number
    $('body').css('font-size', baseFont * 1.2);
    
});
</script>
</body>
</html>
You can change multiple CSS properties at once like this using object literals. You are passing an 'object' to the css() function.

$('p').css({
   'font-size':'200%',
   'background-color': 'yellow'
});

Object literals is a way to create an object with a list of property name/value pairs.
{
property:value,
property:value,
property:value
}
An object literal for two CSS property value looks like this:
{
  'font-size' : '200%',
  'background-color' : 'yellow'
}
Notes

Acting on Each Element in a Selection (p138)

Now we know how to select and then apply jQuery functions like this;
  $('img').fadeOut(); // applies .fadeOut() too all <img> elements

fadeOut() is a jQuery built-in function. What if you want to loop through a selection of elements and perform a series of operations you write yourself (instead of fadeOut)? You use each().

.each() -- iterate over a jQuery object and execute a function for each matched element [jq_02_each.html]
<!DOCTYPE html>
<html>
<head>
<title>jQuery</title>
<script src="http://code.jquery.com/jquery-3.2.1.js"></script>
</head>
<body>

<p> paragraph 1 </p>
<p> paragraph 2 </p>
<p> paragraph 3 </p>

<script>
$('p').each(function(){
   alert( $(this).text() ); // note that this is pure javascript
});
</script>
</body>
</html>


First, you apply the .each() function to your jQuery selection.
$('p').each();   
The each() function takes a function as an argument. That function gets executed for each element that is selected by your jQuery selection.

To do something to what you've selected, you need to pass what is called an anonymous function. The anonymous function describes what is to be done the selection.

It is anonymouse because it is a function that does not have a name.

To pass an anonymous function as an argument to the each() function, first you need to know what an anonymous function looks like:
   function(){
    // code goes here
   }
You literally squeeze in the anonymous function in between the parenthesis as an argument to each()
$('p').each(
   function(){
    // code goes here
   }
);  
Often times, it is organized like this with the all the parenthesis and brackets in one line
$('p').each(function(){
    // code goes here
 });  


this and $(this) -- when using the each() function, you'll want to address each of the elements. To access the current element, you use this or $(this) . this returns a traditional DOM element and $(this) returns a jQuery equivalement. [jq_02_each_this.html]
<!DOCTYPE html>
<html>
<head>
<title>jQuery</title>
<style>
.highlight {
    background: yellow;
  }
</style>
<script src="http://code.jquery.com/jquery-3.2.1.js"></script>
</head>
<body>

<p> paragraph 1 </p>
<p> paragraph 2 </p>
<p> paragraph 3 </p>

<script>
$('p').each(function(){
  $(this).addClass('highlight'); // jquery
  this.innerHTML = "hello";      // JavaScript
});
</script>
</body>
</html>

[jq_02_bibliography.html]
<script>
$(document).ready(function(){

    $('a').each(function(){
     var link = $(this).clone();
     $('#bib').append("- ").append(link).append("<br>");

  });
})
</script>

$(document).ready();

<script>
$(document).ready(

function(){};

);
</script>
Notes

Lab

1.Read Ch4 p131 - p146

2.MM: Ch4 Automatic Pull Quotes (p141 - 146)

3. Here is a script to create 500 buttons. Create a HTML page with this script [or you can copy this one here]. Use jQuery, the jquery each() function, and an anonymous function,
a) number each of the buttons from 0 to 499. (hint, you'll want to take advantage of the global variable to keep track... "var n = 0;").
b) and do something else (change color, change fontsize) to the individual buttons using jquery
<script>
// create 500 buttons.
for(var i=0; i < 500; i++){                  // do not modify
   $('body').append("<button></button>");    // do not modify
}                                            // do not modify
</script>
Notes