Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merging attributes page into Manipulating Elements #713

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
27 changes: 0 additions & 27 deletions page/using-jquery-core/attributes.md

This file was deleted.

16 changes: 12 additions & 4 deletions page/using-jquery-core/manipulating-elements.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,23 +132,31 @@ myList.append( myItems.join( "" ) );

## Manipulating Attributes

jQuery's attribute manipulation capabilities are extensive. Basic changes are simple, but the `.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 [.attr()](https://api.jquery.com/attr/) method also allows for more complex manipulations.
The `.attr()` method acts as both a getter and a setter. As a setter, `.attr()` can accept either a key and a value, or an object containing one or more key/value pairs.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can link back to the relevant page of the API?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AurelioDeRosa good point! I agree that would be useful


```
// Manipulating a single attribute.
// Setting a single attribute.
$( "#myDiv a:first" ).attr( "href", "newDestination.html" );
```

```
// Manipulating multiple attributes.
// Getting a single attribute.
$( "a" ).attr( "href" ); // Returns the href for the first a element in the document
```

```
// Setting multiple attributes.
$( "#myDiv a:first" ).attr({
href: "newDestination.html",
rel: "nofollow"
});
```

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:

```
// Using a function to determine an attribute's new value.
// Using a function to set an attribute's new value.
$( "#myDiv a:first" ).attr({
rel: "nofollow",
href: function( idx, href ) {
Expand Down