Skip to content
Closed
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
1afacaa
style conformance for about-jquery/how-jquery-works
Nov 7, 2012
a98b0bf
style conformance for javascript-101/getting-started
Nov 7, 2012
776c4aa
style conformance for javascript-101/syntax-basics
Nov 7, 2012
f7641f4
style conformance for javascript-101/running-code
Nov 7, 2012
87ce1f2
style conformance for javascript-101/types
Nov 8, 2012
74be2db
style conformance for javascript-101/operators
Nov 8, 2012
cc01ee8
style conformance for javascript-101/conditional-code
Nov 8, 2012
2b4cf16
found the section on truthy/falsy
Nov 8, 2012
70b2003
style conformance for javascript-101/loops
Nov 8, 2012
6b04bb6
style conformance for javascript-101/arrays
Nov 8, 2012
0d7b57e
adding links to objects/arrays sections
Nov 8, 2012
f97c941
style conformance for javascript-101/objects
Nov 8, 2012
47fda0e
style conformance for javascript-101/functions
Nov 8, 2012
e498797
style conformance for javascript-101/testing-type
Nov 8, 2012
f1c8aff
link to testing-type section
Nov 8, 2012
2254121
style conformance for javascript-101/this-keyword
Nov 8, 2012
2dfb23a
style conformance for javascript-101/scope
Nov 8, 2012
1cb18ad
style conformance for javascript-101/closures
Nov 8, 2012
4259929
horizontal scrolling issues
Nov 8, 2012
a709de1
consistency with —
Nov 8, 2012
adcbaeb
style conformance for using-jquery-core/dollar-object-vs-function
Nov 8, 2012
bbe819e
style conformance for using-jquery-core/document-ready
Nov 8, 2012
bc6b21c
style conformance for using-jquery-core/avoid-conflicts-other-libraries
Nov 8, 2012
24b91ce
style conformance for using-jquery-core/attributes
Nov 8, 2012
8989211
style conformance for using-jquery-core/selecting-elements
Nov 8, 2012
d8c1ff5
style conformance for using-jquery-core/working-with-selections
Nov 9, 2012
3f9e54a
style conformance for using-jquery-core/manipulating-elements
Nov 9, 2012
947dff6
style conformance for using-jquery-core/data-methods
Nov 9, 2012
b17307b
style conformance for using-jquery-core/utility-methods
Nov 9, 2012
40c11cf
style conformance for using-jquery-core/jquery-object
Nov 9, 2012
96f06d4
style conformance for using-jquery-core/traversing
Nov 9, 2012
a52fe58
style conformance for using-jquery-core/css-styling-dimensions
Nov 9, 2012
6256c9e
style conformance for using-jquery-core/iterating
Nov 9, 2012
8f96503
style conformance for using-jquery-core/understanding-index
Nov 9, 2012
e512536
deleting obsolete exercises.md page
Nov 9, 2012
d6d5dcb
Update config-sample to reflect local.* instead of dev.*
ajpiano Nov 9, 2012
4ae27eb
Upgrade to grunt-jquery-content 0.5.10, add grunt-check-modules to gr…
ajpiano Nov 9, 2012
495a7b0
README.md updates
ajpiano Nov 9, 2012
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
style conformance for using-jquery-core/manipulating-elements
  • Loading branch information
jorydotcom committed Nov 9, 2012
commit 3f9e54abeeb28e0c5a8ac4332e8533e3ff712690
147 changes: 34 additions & 113 deletions page/using-jquery-core/manipulating-elements.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,81 +5,37 @@ source: http://jqfundamentals.com/legacy
attribution:
- jQuery Fundamentals
---
Once you've made a selection, the fun begins. You can change, move, remove, and clone elements.
You can also create new elements via a simple syntax.
For complete documentation of jQuery manipulation methods, visit the [Manipulation documentation on api.jquery.com](http://api.jquery.com/category/manipulation/).

For complete documentation of jQuery manipulation methods, visit the
[Manipulation documentation on api.jquery.com](http://api.jquery.com/category/manipulation/).
## Getting and Setting Information about Elements

### Getting and Setting Information about Elements
There are many ways to can change an existing element. Among the most common tasks is changing the inner HTML or attribute of an element. jQuery offers simple, cross-browser methods for these sorts of manipulations. You can also get information about elements using many of the same methods in their getter incarnations. For more information on getters and setters, see the [Working with Selections](/working-with-selections) section. Here are a few methods you can use to get and set information about elements:

There are any number of ways you can change an existing element. Among the
most common tasks you'll perform is changing the inner HTML or attribute of an
element. jQuery offers simple, cross-browser methods for these sorts of
manipulations. You can also get information about elements using many of the
same methods in their getter incarnations. We'll see examples of these
throughout this section, but specifically, here are a few methods you can use
to get and set information about elements.
* **`$.fn.html`** - Get or set the html contents.
* **`$.fn.text`** - Get or set the text contents; HTML will be stripped.
* **`$.fn.attr`** - Get or set the value of the provided attribute.
* **`$.fn.width`** - Get or set the width in pixels of the first element in the selection as an integer.
* **`$.fn.height`** - Get or set the height in pixels of the first element in the selection as an integer.
* **`$fn.position`** - Get an object with position information for the first element in the selection, relative to its first positioned ancestor. _This is a getter only_.
* **`$.fn.val`** - Get or set the value of form elements.

<div class="note">
Changing things about elements is trivial, but remember that the change will affect all elements in the selection.
If you just want to change one element, be sure to specify that in the selection before calling a setter method.
</div>

<div class="note">
When methods act as getters, they generally only work on the first element in the selection.
They do not return a jQuery object, so you can't chain additional methods to them.
One notable exception is `$.fn.text`; as mentioned below, it gets the text for all elements in the selection.
</div>

#### $.fn.html
Get or set the html contents.

#### $.fn.text
Get or set the text contents; HTML will be stripped.

#### $.fn.attr
Get or set the value of the provided attribute.

#### $.fn.width
Get or set the width in pixels of the first element in the selection as an integer.

#### .fn.height
Get or set the height in pixels of the first element in the selection as an integer.

#### fn.position
Get an object with position information for the first element in the selection, relative to its first positioned ancestor. _This is a getter only_.

#### $.fn.val
Get or set the value of form elements.
Changing things about elements is trivial, but remember that the change will affect all elements in the selection. If you just want to change one element, be sure to specify that in the selection before calling a setter method.

```
// Changing the HTML of an element
$("#myDiv p:first").html("New <strong>first</strong> paragraph!");
```

### Moving, Copying, and Removing Elements
## Moving, Copying, and Removing Elements

There are a variety of ways to move elements around the DOM; generally, there are two approaches:
While there are a variety of ways to move elements around the DOM, there are generally two approaches:

* Place the selected element(s) relative to another element
* Place the selected element(s) relative to another element.
* Place an element relative to the selected element(s).

* Place an element relative to the selected element(s)
For example, jQuery provides `$.fn.insertAfter` and `$.fn.after`. The `$.fn.insertAfter` method places the selected element(s) after the element that provided as an argument. The `$.fn.after` method places the element provided as an argument after the selected element. Several other methods follow this pattern: `$.fn.insertBefore` and `$.fn.before`, `$.fn.appendTo` and `$.fn.append`, and `$.fn.prependTo` and `$.fn.prepend`.

For example, jQuery provides `$.fn.insertAfter` and `$.fn.after`. The
`$.fn.insertAfter` method places the selected element(s) after the element that
you provide as an argument; the `$.fn.after` method places the element provided
as an argument after the selected element. Several other methods follow this
pattern: `$.fn.insertBefore` and `$.fn.before`; `$.fn.appendTo` and
`$.fn.append`; and `$.fn.prependTo` and `$.fn.prepend`.

The method that makes the most sense for you will depend on what elements you
already have selected, and whether you will need to store a reference to the
elements you're adding to the page. If you need to store a reference, you will
always want to take the first approach — placing the selected elements relative
to another element — as it returns the element(s) you're placing. In this
case, `$.fn.insertAfter`, `$.fn.insertBefore`, `$.fn.appendTo`, and
`$.fn.prependTo` will be your tools of choice.
The method that makes the most sense will depend on what elements are selected, and whether you need to store a reference to the elements you're adding to the page. If you need to store a reference, you will always want to take the first approach &#8212; placing the selected elements relative to another element &#8212; as it returns the element(s) you're placing. In this case, `$.fn.insertAfter`, `$.fn.insertBefore`, `$.fn.appendTo`, and `$.fn.prependTo` should be the tools of choice.

```
// Moving elements using different approaches
Expand All @@ -95,10 +51,9 @@ $("#myList").append( $("#myList li:first") );
// the list itself
```

### Cloning Elements
## Cloning Elements

When you use methods such as `$.fn.appendTo`, you are moving the element; sometimes you want to make a copy of the element instead.
In this case, you'll need to use `$.fn.clone` first.
Methods such as `$.fn.appendTo` move the element, but sometimes a copy of the element is needed instead. In this case, use `$.fn.clone` first:

```
// Making a copy of an element
Expand All @@ -107,36 +62,22 @@ In this case, you'll need to use `$.fn.clone` first.
$("#myList li:first").clone().appendTo("#myList");
```

<div class="note">
If you need to copy related data and events, be sure to pass `true` as an argument to `$.fn.clone`.
</div>

### Removing Elements

There are two ways to remove elements from the page: `$.fn.remove` and
`$.fn.detach`. You'll use `$.fn.remove` when you want to permanently remove
the selection from the page; while the method does return the removed
element(s), those elements will not have their associated data and events
attached to them if you return them to the page.
## Removing Elements

There are two ways to remove elements from the page: `$.fn.remove` and `$.fn.detach`. Use `$.fn.remove` when you want to permanently remove the selection from the page. while `$.fn.remove` does return the removed element(s), those elements will not have their associated data and events attached to them if you return them to the page.

If you need the data and events to persist, you'll want to use `$.fn.detach`
instead. Like `$.fn.remove`, it returns the selection, but it also maintains
the data and events associated with the selection, so you can restore the
selection to the page at a later time.
Use `$.fn.detach` if you need the data and events to persist. Like `$.fn.remove`, it returns the selection, but it also maintains the data and events associated with the selection, so you can restore the selection to the page at a later time.

<div class="note"> The `$.fn.detach` method is extremely valuable
if you are doing heavy manipulation to an element. In that case, it's
beneficial to `$.fn.detach` the element from the page, work on it in your code,
and then restore it to the page when you're done. This saves you from
expensive "DOM touches" while maintaining the element's data and events.
</div>
The `$.fn.detach` method is extremely valuable if you are doing heavy manipulation on an element. In that case, it's beneficial to `$.fn.detach` the element from the page, work on it in your code, then restore it to the page when you're done. This limits expensive "DOM touches" while maintaining the element's data and events.

If you want to leave the element on the page but simply want to remove its
contents, you can use `$.fn.empty` to dispose of the element's inner HTML.
If you want to leave the element on the page but remove its contents, you can use `$.fn.empty` to dispose of the element's inner HTML.

### Creating New Elements
## Creating New Elements

jQuery offers a trivial and elegant way to create new elements using the same `$()` method you use to make selections.
jQuery offers a trivial and elegant way to create new elements using the same `$()` method used to make selections:

```
// Creating new elements from an HTML string
Expand All @@ -154,13 +95,9 @@ $( "<a/>", {
});
```

Note that in the attributes object we included as the second argument, the
property name class is quoted, while the property names text and href are not.
Property names generally do not need to be quoted unless they are reserved
words (as class is in this case).
Note that the attributes object in the second argument above, the property name class is quoted, although the property names 'text' and 'href' are not. Property names generally do not need to be quoted unless they are reserved words (as class is in this case).

When you create a new element, it is not immediately added to the page.
There are several ways to add an element to the page once it's been created.
When you create a new element, it is not immediately added to the page. There are several ways to add an element to the page once it's been created.

```
// Getting a new element on to the page
Expand All @@ -173,27 +110,16 @@ $myNewElement.insertAfter("ul:last"); // this will remove the p from #content!
$("ul").last().after( $myNewElement.clone() ); // clone the p so now we have 2
```

Strictly speaking, you don't have to store the created element in a variable —
you could just call the method to add the element to the page directly after
the `$()`. However, most of the time you will want a reference to the element
you added, so you don't need to select it later.
The created element doesn't need to be stored in a variable &#8212; you can call the method to add the element to the page directly after the `$()`. However, most of the time you'll want a reference to the element you added so you won't have to select it later.

You can even create an element as you're adding it to the page, but note that
in this case you don't get a reference to the newly created element.
You can also create an element as you're adding it to the page, but note that in this case you don't get a reference to the newly created element:

```
// Creating and adding an element to the page at the same time
$("ul").append("<li>list item</li>");
```

<div class="note"> The syntax for adding new elements to the page
is so easy, it's tempting to forget that there's a huge performance cost for
adding to the DOM repeatedly. If you are adding many elements to the same
container, you'll want to concatenate all the html into a single string, and
then append that string to the container instead of appending the elements one
at a time. You can use an array to gather all the pieces together, then join
them into a single string for appending.
</div>
The syntax for adding new elements to the page is easy, so it's tempting to forget that there's a huge performance cost for adding to the DOM repeatedly. If you're adding many elements to the same container, you'll want to concatenate all the html into a single string, and then append that string to the container instead of appending the elements one at a time. Use an array to gather all the pieces together, then join them into a single string for appending.

```
var myItems = [];
Expand All @@ -208,14 +134,9 @@ for ( var i = 0; i < 100; i++ ) {
$myList.append( myItems.join("") );
```

### Manipulating Attributes
## Manipulating Attributes

jQuery's attribute manipulation capabilities are extensive. Basic changes are
simple, but the `$.fn.attr` method also allows for more complex manipulations.
It can either set an explicit value, or set a value using the return value of a
function. When the function syntax is used, the function receives two
arguments: the zero-based index of the element whose attribute is being
changed, and the current value of the attribute being changed.
jQuery's attribute manipulation capabilities are extensive. Basic changes are simple, but the `$.fn.attr` method also allows for more complex manipulations. It can either set an explicit value, or set a value using the return value of a function. When the function syntax is used, the function receives two arguments: the zero-based index of the element whose attribute is being changed, and the current value of the attribute being changed.

```
// Manipulating a single attribute
Expand Down