Skip to content

Commit c476ce6

Browse files
author
Rebecca Murphey
committed
Merge branch 'master' of github.com:jquery/web-learn-jquery-com
* 'master' of github.com:jquery/web-learn-jquery-com: Add attributes and traversing to jquery-basics
2 parents a89497e + b46a77f commit c476ce6

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
chapter : "jqfundamentals"
3+
section : "4"
4+
title : "Attributes"
5+
---
6+
## Attributes
7+
8+
An element's attributes can contain useful information for your application, so 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 `$.fn.css` method, `$.fn.attr` as a setter can accept either a key and a value, or an object containing one or more key/value pairs.
11+
12+
<div class="example" markdown="1">
13+
Setting attributes
14+
15+
$('a').attr('href', 'allMyHrefsAreTheSameNow.html');
16+
$('a').attr({
17+
'title' : 'all titles are the same too!',
18+
'href' : 'somethingNew.html'
19+
});
20+
</div>
21+
22+
This time, we broke the object up into multiple lines. Remember, whitespace doesn't matter in JavaScript, so you should feel free to use it liberally to make your code more legible! You can use a minification tool later to strip out unnecessary whitespace for production.
23+
24+
<div class="example" markdown="1">
25+
Getting attributes
26+
27+
$('a').attr('href'); // returns the href for the first a element in the document
28+
</div>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
chapter : "jqfundamentals"
3+
section : "5"
4+
title : "Traversing"
5+
---
6+
## Traversing
7+
8+
Once you have a jQuery selection, you can find other elements using your selection as a starting point.
9+
10+
For complete documentation of jQuery traversal methods, visit [http://api.jquery.com/category/traversing/](http://api.jquery.com/category/traversing/ "Traversing documentation on api.jquery.com").
11+
12+
<div class="note" markdown="1">
13+
### Note
14+
15+
Be cautious with traversing long distances in your documents — complex traversal makes it imperative that your document's structure remain the same, something that's difficult to guarantee even if you're the one creating the whole application from server to client. One- or two-step traversal is fine, but you generally want to avoid traversals that take you from one container to another.
16+
</div>
17+
18+
<div class="example" markdown="1">
19+
Moving around the DOM using traversal methods
20+
21+
$('h1').next('p');
22+
$('div:visible').parent();
23+
$('input[name=first_name]').closest('form');
24+
$('#myList').children();
25+
$('li.selected').siblings();
26+
</div>
27+
28+
You can also iterate over a selection using `$.fn.each`. This method iterates over all of the elements in a selection, and runs a function for each one. The function receives the index of the current element and the DOM element itself as arguments. Inside the function, the DOM element is also available as `this` by default.
29+
30+
<div class="example" markdown="1">
31+
Iterating over a selection
32+
33+
$('#myList li').each(function(idx, el) {
34+
console.log(
35+
'Element ' + idx +
36+
'has the following html: ' +
37+
$(el).html()
38+
);
39+
});
40+
</div>

0 commit comments

Comments
 (0)