Skip to content

Commit b61492f

Browse files
committed
unify "jQuery Basics" and "Using jQuery Core" into a single chapter. Resolves #86
1 parent 668ee1c commit b61492f

15 files changed

+1043
-8
lines changed

Rules

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ preprocess do
8686
@chapterOrder = [
8787
"getting-started",
8888
"javascript-101",
89-
"jquery-basics",
9089
"using-jquery-core",
9190
"events",
9291
"effects",

content/jquery-basics/dex.md

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
chapter : using-jquery-core
3+
level : beginner
4+
section : 4
5+
title : Attributes
6+
---
7+
An element's attributes can contain useful information for your application, so
8+
it's important to be able to get and set them.
9+
10+
The `$.fn.attr` method acts as both a getter and a setter. As with the
11+
`$.fn.css` method, `$.fn.attr` as a setter can accept either a key and a value,
12+
or an object containing one or more key/value pairs.
13+
14+
<javascript caption="Setting attributes">
15+
$('a').attr('href', 'allMyHrefsAreTheSameNow.html');
16+
$('a').attr({
17+
'title' : 'all titles are the same too!',
18+
'href' : 'somethingNew.html'
19+
});
20+
</javascript>
21+
22+
This time, we broke the object up into multiple lines. Remember, whitespace
23+
doesn't matter in JavaScript, so you should feel free to use it liberally to
24+
make your code more legible! You can use a minification tool later to strip out
25+
unnecessary whitespace for production.
26+
27+
<javascript caption="Getting attributes">
28+
$('a').attr('href'); // returns the href for the first a element in the document
29+
</javascript>

content/using-jquery-core/avoid-conflicts-other-libraries.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
2-
chapter : jquery-core
2+
chapter : using-jquery-core
3+
level: beginner
34
section : 6
45
title : Avoiding Conflicts with Other Libraries
56
attribution: jQuery Fundamentals
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
---
2+
chapter : using-jquery-core
3+
level: beginner
4+
section : 3
5+
title : CSS, Styling, & Dimensions
6+
---
7+
## CSS, Styling, &amp; Dimensions
8+
9+
jQuery includes a handy way to get and set CSS properties of elements.
10+
11+
<div class="note" markdown="1">
12+
CSS properties that normally include a hyphen
13+
need to be camel cased in JavaScript. For example, the CSS property font-size
14+
is expressed as fontSize when used as a property name in JavaScript. This does
15+
not apply, however, when passing the name of a CSS property to the `$.fn.css`
16+
method as a string — in that case, either the camel cased or hyphenated form
17+
will work.
18+
</div>
19+
20+
<javascript caption="Getting CSS properties">
21+
$('h1').css('fontSize'); // returns a string such as "19px"
22+
$('h1').css('font-size'); // also works
23+
</javascript>
24+
25+
<javascript caption="Setting CSS properties">
26+
$('h1').css('fontSize', '100px'); // setting an individual property
27+
$('h1').css({ 'fontSize' : '100px', 'color' : 'red' }); // setting multiple properties
28+
</javascript>
29+
30+
Note the style of the argument we use on the second line — it is an object that
31+
contains multiple properties. This is a common way to pass multiple arguments
32+
to a function, and many jQuery setter methods accept objects to set mulitple
33+
values at once.
34+
35+
**Note:** while we do not recommend using `$.fn.css` as a setter in production-ready code (see below), when passing in an object to set CSS, CSS properties will be camelCased, instead of using a hypen.
36+
37+
### Using CSS Classes for Styling
38+
39+
As a getter, the `$.fn.css` method is valuable; however, it should generally be
40+
avoided as a setter in production-ready code, because you don't want
41+
presentational information in your JavaScript. Instead, write CSS rules for
42+
classes that describe the various visual states, and then simply change the
43+
class on the element you want to affect.
44+
45+
<javascript caption="Working with classes">
46+
var $h1 = $('h1');
47+
48+
$h1.addClass('big');
49+
$h1.removeClass('big');
50+
$h1.toggleClass('big');
51+
52+
if ($h1.hasClass('big')) { ... }
53+
</javascript>
54+
55+
Classes can also be useful for storing state information about an element, such as indicating that an element is selected.
56+
57+
### Dimensions
58+
59+
jQuery offers a variety of methods for obtaining and modifying dimension and position information about an element.
60+
61+
The code in “Basic dimensions methods”, is just a very brief overview of the
62+
dimensions functionality in jQuery; for complete details about jQuery dimension
63+
methods, visit the [Dimensions documentation on api.jquery.com](http://api.jquery.com/category/dimensions/).
64+
65+
<javascript caption="Basic dimensions methods">
66+
$('h1').width('50px'); // sets the width of all H1 elements
67+
$('h1').width(); // gets the width of the first H1
68+
69+
$('h1').height('50px'); // sets the height of all H1 elements
70+
$('h1').height(); // gets the height of the first H1
71+
72+
$('h1').position(); // returns an object containing position
73+
// information for the first H1 relative to
74+
// its "offset (positioned) parent"
75+
</javascript>

content/using-jquery-core/data-methods.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ chapter : jquery-core
33
section : 4
44
title : Data Methods
55
attribution: jQuery Fundamentals
6+
level: intermediate
67
---
78
As your work with jQuery progresses, you'll find that there's often data about
89
an element that you want to store with the element. In plain JavaScript, you
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
---
2+
chapter : jquery-basics
3+
level: beginner
4+
section : 1
5+
title : $(document).ready()
6+
---
7+
You cannot safely manipulate a page until the document is “ready.”
8+
jQuery detects this state of readiness for you; code included inside
9+
`$(document).ready()` will only run once the page DOM (Document Object Model) is ready for JavaScript code to execute
10+
whereas `$(window).load(function () {})` will run once the entire page (images or iframe) not just the DOM is ready.
11+
12+
<javascript caption="A $(document).ready() block">
13+
$(document).ready(function() {
14+
console.log('ready!');
15+
});
16+
</javascript>
17+
18+
There is a shorthand for `$(document).ready()` that you will sometimes see; however,
19+
I recommend against using it if you are writing code that people who aren't experienced
20+
with jQuery may see.
21+
22+
<javascript caption="Shorthand for $(document).ready()">
23+
24+
$(function() {
25+
console.log('ready!');
26+
});
27+
28+
</javascript>
29+
30+
You can also pass a named function to `$(document).ready()` instead of passing an anonymous function.
31+
32+
<javascript caption="Passing a named function instead of an anonymous function">
33+
34+
function readyFn( jQuery ) {
35+
// code to run when the document is ready
36+
}
37+
38+
$(document).ready(readyFn);
39+
40+
// OR
41+
42+
$(window).load(readyFn);
43+
44+
</javascript>
45+
46+
Lets take a look at how both the events act. The below example tries to load some website url in an iframe and checks for both the events.
47+
<javascript>
48+
49+
<html>
50+
<head>
51+
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
52+
<script>
53+
$(document).ready(function () {
54+
console.log('document loaded');
55+
});
56+
57+
$(window).load(function () {
58+
console.log('window loaded');
59+
});
60+
</script>
61+
</head>
62+
<body>
63+
<iframe src='http://techcrunch.com'></iframe>
64+
</body>
65+
</html>
66+
67+
</javascript>

content/using-jquery-core/dollar-object-vs-function.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
---
2-
chapter : jquery-core
2+
chapter : using-jquery-core
33
section : 1
44
title : $ vs $()
55
attribution: jQuery Fundamentals
6+
level: beginner
67
---
78
Until now, we’ve been dealing entirely with methods that are called on a jQuery
89
object. For example:
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
chapter : jquery-basics
3+
section : 7
4+
title : Exercises
5+
attribution: jQuery Fundamentals
6+
---
7+
## Exercises
8+
9+
### Selecting
10+
11+
Open the file `/exercises/index.html` in your browser.
12+
Use the file `/exercises/js/sandbox.js` or work in Firebug to accomplish the following:
13+
14+
1. Select all of the div elements that have a class of "module".
15+
16+
2. Come up with three selectors that you could use to get the third item in the #myList unordered list. Which is the best to use? Why?
17+
18+
3. Select the label for the search input using an attribute selector.
19+
20+
4. Figure out how many elements on the page are hidden (hint: .length).
21+
22+
5. Figure out how many image elements on the page have an alt attribute.
23+
24+
6. Select all of the odd table rows in the table body.
25+
26+
### Traversing
27+
28+
Open the file `/exercises/index.html` in your browser. Use the file `/exercises/js/sandbox.js` or work in Firebug to accomplish the following:
29+
30+
1. Select all of the image elements on the page; log each image's alt attribute.
31+
32+
2. Select the search input text box, then traverse up to the form and add a class to the form.
33+
34+
3. Select the list item inside `#myList` that has a class of "current" and remove that class from it; add a class of "current" to the next list item.
35+
36+
4. Select the select element inside `#specials`; traverse your way to the submit button.
37+
38+
5. Select the first list item in the #slideshow element; add the class "current" to it, and then add a class of "disabled" to its sibling elements.
39+
40+
### Manipulating
41+
42+
Open the file `/exercises/index.html` in your browser. Use the file `/exercises/js/sandbox.js` or work in Firebug to accomplish the following:
43+
44+
1. Add five new list items to the end of the unordered list `#myList`. Hint:
45+
46+
for (var i = 0; i&lt;5; i++) { ... }
47+
48+
2. Remove the odd list items
49+
50+
3. Add another h2 and another paragraph to the last div.module
51+
52+
4. Add another option to the select element; give the option the value "Wednesday"
53+
54+
5. Add a new div.module to the page after the last one; put a copy of one of the existing images inside of it.

content/using-jquery-core/feature-browser-detection.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
---
2-
chapter : jquery-core
2+
chapter : using-jquery-core
33
section : 5
44
title : Feature & Browser Detection
5+
level: intermediate
56
attribution: jQuery Fundamentals
67
---
78
## Feature & Browser Detection

0 commit comments

Comments
 (0)