Last active
September 15, 2017 21:27
-
-
Save ripter/9206fd495f0c709e06c1ab6dd31a328b to your computer and use it in GitHub Desktop.
Attributes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<a href="//github.com">href is an HTML attribute</a> | |
<p style="background-color: orange;">style is also an HTML attribute</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const a = document.getElementsByTagName('a')[0]; // get <a> from the example html | |
const p = document.getElementsByTagName('p')[0]; // get <p> from the example html | |
// Get/Read Attributes | |
a.attributes.href; // returns NamedNodeMap href="//github.com" | |
a.getAttribute('href'); // returns "//github.com" | |
// Set/Write Attributes. | |
p.attributes.style.value = 'color: green;'; // set the attribute value. | |
p.setAttribute('style', 'color: white;'); // set the inline style. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var o = {}; // Creates a new object | |
// Example of an object property added | |
// with defineProperty with a data property descriptor | |
Object.defineProperty(o, 'a', { | |
value: 37, // attribute name/key is 'value', it's value is 37 | |
writable: true, // attribute name/key is 'writable', it's value is true | |
enumerable: true, // attribute name/key is 'enumerable', it's value is true | |
configurable: true, // attribute name/key is 'configurable', it's value is true | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment