Attributes / Properties
Browser Document: Attributes / Properties
What happens to the HTML when the browser loads the page?
View Answer:
What is the difference between attributes and properties in JavaScript?
View Answer:
Can you modify or customize a DOM node?
View Answer:
// Property Creation
document.body.myData = {
name: 'Caesar',
title: 'Imperator',
};
console.log(document.body.myData.title); // Imperator
// Add new method
document.body.sayTagName = function () {
console.log(this.tagName);
};
document.body.sayTagName();
// BODY (the value of "this" in the method is document.body)
// Add new method to all Elements
Element.prototype.sayHi = function () {
console.log(`Hello, I'm ${this.tagName}`);
};
document.documentElement.sayHi(); // Hello, I'm HTML
document.body.sayHi(); // Hello, I'm BODY
DOM properties and methods behave just like those of regular JavaScript objects. They can have any value and are case-sensitive (write elem.nodeType, not elem.NoDeTyPe).
How can you get the value of an attribute in JavaScript?
View Answer:
How can you set an attribute value in JavaScript?
View Answer:
How do you remove an attribute from an HTML element in JavaScript?
View Answer:
What is the role of the "className" property in JavaScript?
View Answer:
What is the "dataset" property in JavaScript?
View Answer:
How can you modify the "src" attribute of an "img" element in JavaScript?
View Answer:
Do different elements have different standard HTML attributes?
View Answer:
<body id="body" type="...">
<input id="input" type="text" />
<script>
console.log(input.type); // text
console.log(body.type);
// undefined: DOM property not created, because it is non-standard
</script>
</body>
The "type" attribute is standard for ‹input› (HTMLInputElement), but not for ‹body› (HTMLBodyElement). Standard attributes are described in the specification for the corresponding element class. So, if an attribute is non-standard, there will not be a DOM-property for it.
What happens to non-standard attributes when the browser loads the page? Does it become a DOM property like standard element attributes?
View Answer:
The attribute exists, but it does not get defined as DOM property, which inevitably returns undefined.
<body id="test" something="non-standard">
<script>
console.log(document.body.id); // test
// non-standard attribute does not yield a property
console.log(document.body.something); // undefined
</script>
</body>
Is there a way to access non-standard HTML attributes?
View Answer:
<body something="non-standard">
<script>
console.log(document.body.getAttribute('something')); // non-standard
</script>
</body>
What are the two primary features of HTML attributes?
View Answer:
<body>
<div id="elem" about="Elephant"></div>
<script>
console.log(elem.getAttribute('About')); // (1) 'Elephant', reading
elem.setAttribute('Test', 123); // (2), writing
console.log(elem.outerHTML); // (3), see if the attribute is in HTML (yes)
for (let attr of elem.attributes) {
// (4) list all
console.log(`${attr.name} = ${attr.value}`);
}
</script>
</body>
What happens to the related property when a standard attribute changes?
View Answer:
<input />
<script>
let input = document.querySelector('input');
// attribute => property
input.setAttribute('id', 'id');
console.log(input.id); // id (updated)
// property => attribute
input.id = 'newId';
console.log(input.getAttribute('id')); // newId (updated)
</script>
Can you name one exception to the rule of property-attribute synchronization?
View Answer:
<script>
let input = document.querySelector('input');
// attribute => property
input.setAttribute('value', 'text');
console.log(input.value); // text
// NOT property => attribute
input.value = 'newValue';
console.log(input.getAttribute('value')); // text (not updated!)
</script>
That “feature” may come in handy because the user actions may lead to value changes, and then after them, if we want to recover the “original” value from HTML, it is in the attribute.
Are DOM properties always strings like HTML attributes?
View Answer:
<!-- CHECKBOX EXAMPLE -->
<input id="input" type="checkbox" checked />
<script>
console.log(input.getAttribute('checked')); // the attribute value is: empty string
console.log(input.checked); // the property value is: true
</script>
<!-- STYLE PROPERTY EXAMPLE -->
<div id="div" style="color:red;font-size:120%">Hello</div>
<script>
// string
console.log(div.getAttribute('style')); // color:red;font-size:120%
// object
console.log(div.style); // [object CSSStyleDeclaration]
console.log(div.style.color); // red
</script>
Can you describe a case for the use of non-standard attributes?
View Answer:
<!-- mark the div to show "name" here -->
<div show-info="name"></div>
<!-- and age here -->
<div show-info="age"></div>
<script>
// the code finds an element with the mark and shows what's requested
let user = {
name: 'Pete',
age: 25,
};
for (let div of document.querySelectorAll('[show-info]')) {
// insert the corresponding info into the field
let field = div.getAttribute('show-info');
div.innerHTML = user[field]; // first Pete into "name", then 25 into "age"
}
</script>
Is it possible to style an element with non-standard HTML attributes?
View Answer:
<style>
/* styles rely on the custom attribute "order-state" */
.order[order-state='new'] {
color: green;
}
.order[order-state='pending'] {
color: blue;
}
.order[order-state='canceled'] {
color: red;
}
</style>
<div class="order" order-state="new">A new order.</div>
<div class="order" order-state="pending">A pending order.</div>
<div class="order" order-state="canceled">A canceled order.</div>
We should note that this is not exactly the recommended approach for implementing custom attributes in HTML.
Is there a way to avoid conflicts when creating custom attributes?
View Answer:
<body data-about="Elephants">
<script>
console.log(document.body.dataset.about); // Elephants
</script>
</body>
Are multi-word attributes case sensitive in dataset properties?
View Answer:
<style>
.order[data-order-state='new'] {
color: green;
}
.order[data-order-state='pending'] {
color: blue;
}
.order[data-order-state='canceled'] {
color: red;
}
</style>
<div id="order" class="order" data-order-state="new">A new order.</div>
<script>
// read
console.log(order.dataset.orderState); // new
// modify
order.dataset.orderState = 'pending'; // (*) camel case dataset property
</script>