📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials jQuery in Practice Attributes and Data

Attributes and Data

4 min read
attr() reads and sets HTML attributes. Use prop() for boolean attributes like checked and disabled. data() stores custom values on elements using jQuery's internal cache and reads HTML5 data-* attributes.

Attributes

$('img').attr('src');                 // get
$('img').attr('src', 'photo.jpg');   // set
$('img').removeAttr('alt');

// HTML5 data attributes
$('#btn').data('user-id');            // get data-user-id
$('#btn').data('user-id', 42);        // set in memory
$('#btn').removeData('user-id');