Skip to content

Commit 9af310d

Browse files
committed
Converted code examples in ajax and effects
1 parent 5160f6b commit 9af310d

11 files changed

+89
-71
lines changed

page/ajax/ajax-and-forms.md

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,24 @@ Serializing form inputs in jQuery is extremely easy. Two methods come supported
1010

1111
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.
1212

13-
14-
<javascript caption="Turning form data into a query string">
13+
```
14+
// Turning form data into a query string
1515
$('#myForm').serialize(); // creates a query string like this: field_1=something&field2=somethingElse
16-
</javascript>
16+
17+
```
1718

1819
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.
1920

20-
<javascript caption="Creating an array of objects containing form data">
21+
```
22+
// Creating an array of objects containing form data
2123
$('#myForm').serializeArray();
2224
2325
// creates a structure like this:
2426
// [
2527
// { name : 'field_1', value : 'something' },
2628
// { name : 'field_2', value : 'somethingElse' }
2729
// ]
28-
</javascript>
30+
```
2931

3032
### Client-side validation
3133
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.
@@ -34,7 +36,8 @@ Please note that it is advisable that you also perform server-side validation fo
3436

3537
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.
3638

37-
<javascript caption="Using validation to check for the presence of an input">
39+
```
40+
// Using validation to check for the presence of an input
3841
$("#form").submit(function( e ) {
3942
if ( $(".required").val().length === 0 ) { // if .required's value's length is zero
4043
// usually show some kind of error message here
@@ -44,11 +47,12 @@ $("#form").submit(function( e ) {
4447
// run $.ajax here
4548
}
4649
});
47-
</javascript>
50+
```
4851

4952
Let's see how easy it is to check for invalid characters in a username:
5053

51-
<javascript caption="Validate a phone number field">
54+
```
55+
// Validate a phone number field
5256
$("#form").submit(function( e ) {
5357
var inputtedPhoneNumber = $("#phone").val()
5458
, phoneNumberRegex = ^\d*$/; // match only numbers
@@ -61,7 +65,7 @@ $("#form").submit(function( e ) {
6165
// run $.ajax here
6266
}
6367
})
64-
</javascript>
68+
```
6569

6670

6771

@@ -70,19 +74,21 @@ A prefilter is a way to modify the ajax options before each request is sent (hen
7074

7175
For example, say we would like to modify all crossDomain requests through a proxy. To do so with a prefilter is quite simple:
7276

73-
<javascript caption="Using a proxy with a prefilter">
77+
```
78+
// Using a proxy with a prefilter
7479
$.ajaxPrefilter(function( options, originalOptions, jqXHR ) {
7580
if ( options.crossDomain ) {
7681
options.url = "http://mydomain.net/proxy/" + encodeURIComponent(options.url );
7782
options.crossDomain = false;
7883
}
7984
});
80-
</javascript>
85+
```
8186

8287
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:
8388

84-
<javascript caption="using the optional dataTypes argument">
89+
```
90+
// Using the optional dataTypes argument">
8591
$.ajaxPrefilter( "json script", function( options, originalOptions, jqXHR ) {
8692
// do all of the prefiltering here, but only for requests that indicate a dataType of "JSON" or "script"
8793
})
88-
</javascript>
94+
```

page/ajax/ajax-events.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ this behavior inside every Ajax request, you can bind Ajax events to elements
88
just like you'd bind other events. For a complete list of Ajax events, visit
99
[ Ajax Events documentation on docs.jquery.com ]( http://docs.jquery.com/Ajax_Events ).
1010

11-
<javascript caption="Setting up a loading indicator using Ajax Events">
11+
```
12+
// Setting up a loading indicator using Ajax Events
1213
$('#loading_indicator')
1314
.ajaxStart(function() { $(this).show(); })
1415
.ajaxStop(function() { $(this).hide(); });
15-
</javascript>
16+
```

page/ajax/ajax-excercises.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ need to leverage the href of that link to get the proper content from
2020
blog.html. Once you have the href, here's one way to process it into an ID
2121
that you can use as a selector in `$.fn.load`:
2222

23-
<javascript>
23+
```
2424
var href = 'blog.html#post1';
2525
var tempArray = href.split('#');
2626
var id = '#' + tempArray[1];
27-
</javascript>
27+
```
2828

2929
Remember to make liberal use of `console.log` to make sure you're on the right
3030
path!

page/ajax/jquery-ajax-methods.md

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ documentation of the configuration options, visit
2323
[http://api.jquery.com/jQuery.ajax/](http://api.jquery.com/jQuery.ajax/ "$.ajax
2424
documentation on api.jquery.com").
2525

26-
<javascript caption="Using the core `$.ajax` method">
26+
```
27+
// Using the core `$.ajax` method
2728
$.ajax({
2829
// the URL for the request
2930
url : 'post.php',
@@ -58,9 +59,9 @@ $.ajax({
5859
alert('The request is complete!');
5960
}
6061
});
61-
</javascript>
62+
```
6263

63-
<div class="note" markdown="1">
64+
<div class="note" >
6465
### Note
6566

6667
A note about the dataType setting: if the server sends back data that is in a
@@ -191,7 +192,7 @@ The URL for the request. Required.
191192
The data to be sent to the server. Optional. This can either be an object or a
192193
query string, such as `foo=bar&amp;baz=bim`.
193194

194-
<div class="note" markdown="1">
195+
<div class="note">
195196
### Note
196197

197198
This option is not valid for `$.getScript`.
@@ -208,13 +209,14 @@ object.
208209

209210
The type of data you expect back from the server. Optional.
210211

211-
<div class="note" markdown="1">
212+
<div class="note">
212213
### Note
213214

214215
This option is only applicable for methods that don't already specify the data
215216
type in their name. </div>
216217

217-
<javascript caption="Using jQuery's Ajax convenience methods">
218+
```
219+
// Using jQuery's Ajax convenience methods
218220
// get plain text or html
219221
$.get('/users.php', { userId : 1234 }, function(resp) {
220222
console.log(resp);
@@ -231,7 +233,7 @@ $.getJSON('/details.php', function(resp) {
231233
console.log(k + ' : ' + v);
232234
});
233235
});
234-
</javascript>
236+
```
235237

236238
### `$.fn.load`
237239

@@ -241,12 +243,14 @@ uses the returned HTML to populate the selected element(s). In addition to
241243
providing a URL to the method, you can optionally provide a selector; jQuery
242244
will fetch only the matching content from the returned HTML.
243245

244-
<javascript caption="Using `$.fn.load` to populate an element">
246+
```
247+
// Using `$.fn.load` to populate an element
245248
$('#newContent').load('/foo.html');
246-
</javascript>
249+
```
247250

248-
<javascript caption="Using `$.fn.load` to populate an element based on a selector">
251+
```
252+
// Using `$.fn.load` to populate an element based on a selector
249253
$('#newContent').load('/foo.html #myDiv h1:first', function(html) {
250254
alert('Content updated!');
251255
});
252-
</javascript>
256+
```

page/ajax/key-concepts.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ For adding a new script to the page
4646

4747
For transporting JSON-formatted data, which can include strings, arrays, and objects
4848

49-
<div class="note" markdown="1">
49+
<div class="note">
5050
### Note
5151

5252
As of jQuery 1.4, if the JSON data sent by your server isn't properly
@@ -74,19 +74,19 @@ Ajax calls are asynchronous by default, the response is not immediately
7474
available. Responses can only be handled using a callback. So, for example,
7575
the following code will not work:
7676

77-
<javascript>
77+
```
7878
var response;
7979
$.get('foo.php', function(r) { response = r; });
8080
console.log(response); // undefined!
81-
</javascript>
81+
```
8282

8383
Instead, we need to pass a callback function to our request; this callback will
8484
run when the request succeeds, at which point we can access the data that it
8585
returned, if any.
8686

87-
<javascript>
87+
```
8888
$.get('foo.php', function(response) { console.log(response); });
89-
</javascript>
89+
```
9090

9191
### Same-Origin Policy and JSONP
9292

page/ajax/working-with-jsonp.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ particularly great source of JSONP-formatted data is the [Yahoo! Query
99
Language](http://developer.yahoo.com/yql/console/), which we'll use in the
1010
following example to fetch news about cats.
1111

12-
<javscript caption="Using YQL and JSONP">
12+
```
13+
// Using YQL and JSONP
1314
$.ajax({
1415
url : 'http://query.yahooapis.com/v1/public/yql',
1516
@@ -31,7 +32,7 @@ $.ajax({
3132
console.log(response);
3233
}
3334
});
34-
</javscript>
35+
```
3536

3637
jQuery handles all the complex aspects of JSONP behind-the-scenes — all we have
3738
to do is tell jQuery the name of the JSONP callback parameter specified by YQL

page/effects/built-in-effects.md

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,43 +33,45 @@ Hide the selected elements with a vertical sliding motion.
3333
Show or hide the selected elements with a vertical sliding motion, depending on
3434
whether the elements are currently visible.
3535

36-
<javascript caption="A basic use of a built-in effect">
36+
```
37+
// A basic use of a built-in effect
3738
$('h1').show();
38-
</javascript>
3939
4040
### Changing the Duration of Built-in Effects
4141
4242
With the exception of `$.fn.show` and `$.fn.hide`, all of the built-in methods
4343
are animated over the course of 400ms by default. Changing the duration of an
4444
effect is simple.
4545
46-
<javascript caption="Setting the duration of an effect">
46+
```
47+
// Setting the duration of an effect">
4748
$('h1').fadeIn(300); // fade in over 300ms
4849
$('h1').fadeOut('slow'); // using a built-in speed definition
49-
</javascript>
50+
```
5051
5152
### jQuery.fx.speeds
5253
5354
jQuery has an object at `jQuery.fx.speeds` that contains the default speed, as
5455
well as settings for "`slow`" and "`fast`".
5556
56-
<javascript>
57+
```
5758
speeds: {
5859
slow: 600,
5960
fast: 200,
6061
// Default speed
6162
_default: 400
6263
}
63-
</javascript>
64+
```
6465
6566
It is possible to override or add to this object. For example, you may want to
6667
change the default duration of effects, or you may want to create your own
6768
effects speed.
6869
69-
<javascript caption="Augmenting `jQuery.fx.speeds` with custom speed definitions">
70+
```
71+
// Augmenting `jQuery.fx.speeds` with custom speed definitions">
7072
jQuery.fx.speeds.blazing = 100;
7173
jQuery.fx.speeds.turtle = 2000;
72-
</javascript>
74+
```
7375
7476
### Doing Something when an Effect is Done
7577
@@ -82,15 +84,17 @@ conclusion of the animation. Inside of the callback function, the keyword this
8284
refers to the element that the effect was called on; as we did inside of event
8385
handler functions, we can turn it into a jQuery object via $(this).
8486
85-
<javascript caption="Running code when an animation is complete">
87+
```
88+
// Running code when an animation is complete">
8689
$('div.old').fadeOut(300, function() { $(this).remove(); });
87-
</javascript>
90+
```
8891
8992
Note that if your selection doesn't return any elements, your callback will
9093
never run! You can solve this problem by testing whether your selection
9194
returned any elements; if not, you can just run the callback immediately.
9295
93-
<javascript caption="Run a callback even if there were no elements to animate">
96+
```
97+
// Run a callback even if there were no elements to animate
9498
var $thing = $('#nonexistent');
9599

96100
var cb = function() {
@@ -102,4 +106,4 @@ returned any elements; if not, you can just run the callback immediately.
102106
} else {
103107
cb();
104108
}
105-
</javascript>
109+
```

page/effects/custom-effects.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ jQuery makes it possible to animate arbitrary CSS properties via the
66
`$.fn.animate` method. The `$.fn.animate` method lets you animate to a set
77
value, or to a value relative to the current value.
88

9-
<javascript caption="Custom effects with `$.fn.animate`">
9+
```
10+
// Custom effects with `$.fn.animate`">
1011
$('div.funtimes').animate(
1112
{
1213
left : "+=50",
@@ -15,9 +16,9 @@ value, or to a value relative to the current value.
1516
300, // duration
1617
function() { console.log('done!'); // calback
1718
});
18-
</javascript>
19+
```
1920

20-
<div class="note" markdown="1">
21+
<div class="note">
2122
Color-related properties cannot be animated with `$.fn.animate` using jQuery
2223
out of the box. Color animations can easily be accomplished by including the
2324
[color plugin](http://github.com/jquery/jquery-color). We'll discuss using
@@ -34,15 +35,16 @@ natural transitions in your animations, various easing plugins are available.
3435
As of jQuery 1.4, it is possible to do per-property easing when using the
3536
`$.fn.animate` method.
3637

37-
<javascript caption="Per-property easing">
38+
```
39+
// Per-property easing">
3840
$('div.funtimes').animate(
3941
{
4042
left : [ "+=50", "swing" ],
4143
opacity : [ 0.25, "linear" ]
4244
},
4345
300
4446
);
45-
</javascript>
47+
```
4648

4749
For more details on easing options, see
4850
[Animation documentation on api.jquery.com](http://api.jquery.com/animate/).

page/effects/managing-effects.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ Stop currently running animations on the selected elements.
1414

1515
Wait the specified number of milliseconds before running the next animation.
1616

17-
<javascript>
17+
```
1818
$('h1').show(300).delay(1000).hide(300);
19-
</javascript>
19+
```
2020

2121
### `jQuery.fx.off`
2222

0 commit comments

Comments
 (0)