Skip to content

Commit 64b4d2c

Browse files
committed
Merge pull request jquery#56 from connormontgomery/master
Ajax-and-forms content; code formatting issue fixes; etc
2 parents 5bcc3f6 + 16d5856 commit 64b4d2c

File tree

12 files changed

+129
-60
lines changed

12 files changed

+129
-60
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ This site consists of content maintained in [Markdown](http://daringfireball.net
2323

2424
The entire site is managed via [this Git repository](https://github.com/jquery/learn.jquery.com). If you'd like to contribute new articles, make edits to existing content, or work on the site itself, the first thing you'll need is a [fork](http://help.github.com/fork-a-repo/). When you have changes you'd like to have reviewed and integrated into the site, submit a [pull request](http://help.github.com/send-pull-requests/).
2525

26-
If you aren't already familiar with Git, you'll still need a fork and a GitHub account, but you can can edit files directly via [GitHub's in-browser editor](https://github.com/blog/905-edit-like-an-ace), but you won't be able to create new content. We encourage you to [learn how to use Git andGitHub](http://help.github.com/), it'll probably pretty useful no matter what.
26+
If you aren't already familiar with Git, you'll still need a fork and a GitHub account, but you can can edit files directly via [GitHub's in-browser editor](https://github.com/blog/905-edit-like-an-ace), but you won't be able to create new content. We encourage you to [learn how to use Git and GitHub](http://help.github.com/), it'll probably pretty useful no matter what.
2727

2828

2929
## How Do I Get This Running Locally?

Rules

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class Nanoc3::Filter
2929
languages = LANGUAGES.keys.join("|")
3030

3131
until @string.empty?
32-
match = scan_until /(\+(\S.*?\S?)\+|<(#{languages})(?: filename=["']([^"']*)["'])?(?: caption=["']([^"']*)["'])?>|\z)/m
32+
match = scan_until /(\+(\S.*?\S?)\+|<(#{languages})(?: filename=["']([^"']*)["'])?(?: caption=["]([^"]*)["])?>|\z)/m
3333

3434
@pending << match.pre_match
3535

content/ajax/ajax-and-forms.md

+76-12
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,89 @@ chapter : ajax
33
section : 4
44
title : Ajax and Forms
55
attribution: jQuery Fundamentals
6+
67
---
7-
jQuery’s ajax capabilities can be especially useful when dealing with forms.
8-
The [jQuery Form Plugin](http://jquery.malsup.com/form/) is a well-tested tool
9-
for adding Ajax capabilities to forms, and you should generally use it for
10-
handling forms with Ajax rather than trying to roll your own solution for
11-
anything remotely complex. That said, there are a two jQuery methods you
12-
should know that relate to form processing in jQuery: `$.fn.serialize` and
13-
`$.fn.serializeArray`.
8+
9+
jQuery’s ajax capabilities can be especially useful when dealing with forms. There are several advantages, which can range from serialization, to simple client-side validation (e.g. "Sorry, that username is taken"), to [prefilters](http://api.jquery.com/extending-ajax/#Prefilters) (explained below), and even more!
10+
11+
### Serialization
12+
Serializing form inputs in jQuery is extremely easy. Two methods come supported natively - `$.fn.serialize` and `$.fn.serializeArray`. While the names are fairly self-explanatory, there are many advantages to using them.
13+
14+
The `serialize` method serializes a form's data into a query string. For the element's value to be serialized, it **must** have a `name` attribute. Please noted that values from inputs with a type of `checkbox` or `radio` are included only if they are checked.
15+
1416

1517
<javascript caption="Turning form data into a query string">
16-
$('#myForm').serialize();
18+
$('#myForm').serialize(); // creates a query string like this: field_1=something&field2=somethingElse
1719
</javascript>
1820

21+
While plain old serialization is great, sometimes your application would work better if you sent over an array of objects, instead of just the query string. For that, jQuery has the `serializeArray` method. It's very similar to the `serialize` method listed above, except it produces an array of objects, instead of a string.
22+
1923
<javascript caption="Creating an array of objects containing form data">
2024
$('#myForm').serializeArray();
2125

2226
// creates a structure like this:
23-
[
24-
{ name : 'field1', value : 123 },
25-
{ name : 'field2', value : 'hello world' }
26-
]
27+
// [
28+
// { name : 'field_1', value : 'something' },
29+
// { name : 'field_2', value : 'somethingElse' }
30+
// ]
31+
</javascript>
32+
33+
### Client-side validation
34+
Client-side validation is, much like many other things, extremely easy using jQuery. While there are several cases developers can test for, some of the most common ones are: presence of a required input, valid usernames/emails/phone numbers/etc..., or checking an "I agree..." box.
35+
36+
Please note that it is advisable that you also perform server-side validation for your inputs. However, it typically makes for a better user experience to be able to validate some things without submitting the form.
37+
38+
With that being said, let's jump on in to some examples! First, we'll see how easy it is to check if a required field doesn't have anything in it. If it doesn't, then we'll `return false`, and prevent the form from processing.
39+
40+
<javascript caption="Using validation to check for the presence of an input">
41+
$("#form").submit(function( e ) {
42+
if ( $(".required").val().length === 0 ) { // if .required's value's length is zero
43+
// usually show some kind of error message here
44+
return false; // this prevents the form from submitting
45+
}
46+
else {
47+
// run $.ajax here
48+
}
49+
});
2750
</javascript>
51+
52+
Let's see how easy it is to check for invalid characters in a username:
53+
54+
<javascript caption="Validate a phone number field">
55+
$("#form").submit(function( e ) {
56+
var inputtedPhoneNumber = $("#phone").val()
57+
, phoneNumberRegex = ^\d*$/; // match only numbers
58+
59+
if ( !phoneNumberRegex.test( inputtedPhoneNumber ) ) { // if the phone number doesn't match the regex
60+
// usually show some kind of error message ere
61+
return false; // prevent the form from submitting
62+
}
63+
else {
64+
// run $.ajax here
65+
}
66+
})
67+
</javascript>
68+
69+
70+
71+
### Prefiltering
72+
A prefilter is a way to modify the ajax options before each request is sent (hence, the name `prefilter`).
73+
74+
For example, say we would like to modify all crossDomain requests through a proxy. To do so with a prefilter is quite simple:
75+
76+
<javascript caption="Using a proxy with a prefilter">
77+
$.ajaxPrefilter(function( options, originalOptions, jqXHR ) {
78+
if ( options.crossDomain ) {
79+
options.url = "http://mydomain.net/proxy/" + encodeURIComponent(options.url );
80+
options.crossDomain = false;
81+
}
82+
});
83+
</javascript>
84+
85+
You can pass in an optional argument before the callback function that specifies which `dataTypes` you'd like the prefilter to be applied to. For example, if we want our prefilter to only apply to `JSON` and `script` requests, we'd do:
86+
87+
<javascript caption="using the optional dataTypes argument">
88+
$.ajaxPrefilter( "json script", function( options, originalOptions, jqXHR ) {
89+
// do all of the prefiltering here, but only for requests that indicate a dataType of "JSON" or "script"
90+
})
91+
</javascript>

content/getting-started/downloading-jquery.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ The minified versions, while having a larger file size than the packed versions
8888
## jQuery Git - An Instant WIP Build For Testing
8989

9090
This work-in-progress build (known as jQuery Git) is generated once a minute
91-
from the [ jQuery Git repository ]( http://github.com/jquery/jquery ). It is
91+
from the [jQuery Git repository]( http://github.com/jquery/jquery ). It is
9292
provided as a convenience for anyone that wants to help test changes in the
9393
next version of jQuery.
9494

@@ -135,7 +135,7 @@ jQuery currently requires the following components to be installed:
135135
** ant: Available on any platform with JDK and ANT installed
136136
* java: A copy of Java, version 1.6.0 or later (required to build the minified version of jQuery).
137137

138-
** Build Process **
138+
**Build Process**
139139

140140
You will now need to use the build system that you chose previously - either <code>make</code> or <code>ant</code>.
141141

content/javascript-101/arrays.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,16 @@ console.log(myArray.length); // logs 2
2424

2525
<javascript caption="Changing the value of an array item">
2626
var myArray = [ 'hello', 'world' ];
27-
myArray[1] = 'changed';
27+
myArray[1] = 'changed'; // myArray is now now ['hello', 'changed']
2828
</javascript>
2929

3030
<javascript caption="Adding elements to an array">
3131
var myArray = [ 'hello', 'world' ];
32-
myArray.push('new');
32+
myArray.push('new'); // myArray is now ['hello', 'world', 'new']
3333
</javascript>
3434

3535
<javascript caption="Working with arrays">
3636
var myArray = [ 'h', 'e', 'l', 'l', 'o' ];
37-
var myString = myArray.join(''); // 'hello'
38-
var mySplit = myString.split(''); // [ 'h', 'e', 'l', 'l', 'o' ]
37+
var myString = myArray.join(''); // myString = 'hello'
38+
var mySplit = myString.split(''); // mySPlit = [ 'h', 'e', 'l', 'l', 'o' ]
3939
</javascript>

content/javascript-101/functions.md

+27-28
Original file line numberDiff line numberDiff line change
@@ -25,32 +25,32 @@ used in others' JavaScript code.
2525
## Using Functions
2626

2727
<javascript caption="A simple function">
28-
var greet = function(person, greeting) {
29-
var text = greeting + ', ' + person;
30-
console.log(text);
31-
};
28+
var greet = function(person, greeting) {
29+
var text = greeting + ', ' + person;
30+
console.log(text);
31+
};
3232

33-
greet('Rebecca', 'Hello');
33+
greet('Rebecca', 'Hello');
3434
</javascript>
3535

3636

3737
<javascript caption="A function that returns a value">
38-
var greet = function(person, greeting) {
39-
var text = greeting + ', ' + person;
40-
return text;
41-
};
38+
var greet = function(person, greeting) {
39+
var text = greeting + ', ' + person;
40+
return text;
41+
};
4242

43-
console.log(greet('Rebecca','hello'));
43+
console.log(greet('Rebecca','hello'));
4444
</javascript>
4545

4646
<javascript caption="A function that returns another function">
47-
var greet = function(person, greeting) {
48-
var text = greeting + ', ' + person;
49-
return function() { console.log(text); };
50-
};
47+
var greet = function(person, greeting) {
48+
var text = greeting + ', ' + person;
49+
return function() { console.log(text); };
50+
};
5151

52-
var greeting = greet('Rebecca', 'Hello');
53-
greeting();
52+
var greeting = greet('Rebecca', 'Hello');
53+
greeting();
5454
</javascript>
5555

5656
## Immediately-Invoked Function Expression
@@ -62,11 +62,11 @@ polluting the global namespace with your code — no variables declared inside o
6262
the function are visible outside of it.
6363

6464
<javascript caption="An immediately-invoked function expression">
65-
(function(){
66-
var foo = 'Hello world';
67-
})();
65+
(function(){
66+
var foo = 'Hello world';
67+
})();
6868

69-
console.log(foo); // undefined!
69+
console.log(foo); // undefined!
7070
</javascript>
7171

7272
## Functions as Arguments
@@ -76,16 +76,15 @@ to variables or passed to other functions as arguments. Passing functions as
7676
arguments is an extremely common idiom in jQuery.
7777

7878
<javascript caption="Passing an anonymous function as an argument">
79-
var myFn = function(fn) {
80-
var result = fn();
81-
console.log(result);
82-
};
79+
var myFn = function(fn) {
80+
var result = fn();
81+
console.log(result);
82+
};
8383

84-
myFn(function() { return 'hello world'; }); // logs 'hello world'
84+
myFn(function() { return 'hello world'; }); // logs 'hello world'
8585
</javascript>
8686

87-
<div class="example" markdown="1">
88-
Passing a named function as an argument
87+
<javascript caption="Passing a named function as an argument">
8988

9089
var myFn = function(fn) {
9190
var result = fn();
@@ -97,4 +96,4 @@ Passing a named function as an argument
9796
};
9897

9998
myFn(myOtherFn); // logs 'hello world'
100-
</div>
99+
</javascript>

content/javascript-101/loops.md

+1
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ the loop's body with the break statement.
128128
You may also want to continue the loop without executing more of the loop's
129129
body. This is done using the continue statement.
130130

131+
131132
<javascript caption="Skipping to the next iteration of a loop">
132133
for (var i = 0; i < 10; i++) {
133134
if (something) {

content/javascript-101/objects.md

+7-4
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,26 @@ called a method of the object. Otherwise, they are called properties.
1212
As it turns out, nearly everything in JavaScript is an object -- arrays,
1313
functions, numbers, even strings -- and they all have properties and methods.
1414

15-
<javascript caption="Creating an 'object literal'">
15+
16+
<javascript caption="Creating an object literal">
1617
var myObject = {
1718
sayHello : function() {
1819
console.log('hello');
1920
},
20-
2121
myName : 'Rebecca'
2222
};
23-
23+
2424
myObject.sayHello(); // logs 'hello'
25+
2526
console.log(myObject.myName); // logs 'Rebecca'
2627
</javascript>
2728

29+
2830
When creating object literals, you should note that the key portion of each
2931
key-value pair can be written as any valid JavaScript identifier, a string
3032
(wrapped in quotes) or a number:
31-
<javascript>
33+
34+
<javascript caption="test">
3235
var myObject = {
3336
validIdentifier : 123,
3437
'some string' : 456,

content/javascript-101/scope.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ sayHello(); // logs 'hello'
5353
console.log(foo); // logs 'world'
5454
</javascript>
5555

56-
<javascript caption="Functions can "see" changes in variable values after the function is defined">
56+
<javascript caption="Functions can see changes in variable values after the function is defined">
5757
var myFunction = function() {
5858
var foo = 'hello';
5959

content/javascript-101/testing-type.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var myFunction = function() {
1717
};
1818

1919
var myObject = {
20-
foo : 'bar'
20+
foo : 'bar'
2121
};
2222

2323
var myArray = [ 'a', 'b', 'c' ];

content/jquery-basics/css-styling-dimensions.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ contains multiple properties. This is a common way to pass multiple arguments
3131
to a function, and many jQuery setter methods accept objects to set mulitple
3232
values at once.
3333

34+
**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.
35+
3436
### Using CSS Classes for Styling
3537

3638
As a getter, the `$.fn.css` method is valuable; however, it should generally be
@@ -39,15 +41,15 @@ presentational information in your JavaScript. Instead, write CSS rules for
3941
classes that describe the various visual states, and then simply change the
4042
class on the element you want to affect.
4143

42-
<javscript caption="Working with classes">
44+
<javascript caption="Working with classes">
4345
var $h1 = $('h1');
4446

4547
$h1.addClass('big');
4648
$h1.removeClass('big');
4749
$h1.toggleClass('big');
4850

4951
if ($h1.hasClass('big')) { ... }
50-
</javscript>
52+
</javascript>
5153

5254
Classes can also be useful for storing state information about an element, such as indicating that an element is selected.
5355

layouts/footer.html

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
<h3><span>Quick Access</span></h3>
1010
<div class="cdn">
1111
<strong>CDN</strong>
12-
<span>//ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js</span>
12+
<span>//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js</span>
1313
</div>
1414
<div class="download">
15-
<strong>Download jQuery 1.5.1:</strong>
16-
<span><a href="#">Minified <em>(29KB)</em></a>
17-
<a href="#">Unminified <em>(212KB)</em></a></span>
15+
<strong>Download jQuery 1.7.1:</strong>
16+
<span><a href="//code.jquery.com/jquery-1.7.1.min.js">Minified <em>(29KB)</em></a>
17+
<a href="//code.jquery.com/jquery-1.7.1.js">Unminified <em>(212KB)</em></a></span>
1818
</div>
1919
<ul class="footer-icon-links">
2020
<li class="footer-icon icon-github"><a href="http://github.com/jquery/jquery">Github <small>jQuery <br>Source</small></a></li>

0 commit comments

Comments
 (0)