Sitemap
Elvis Edeh

Elvis Edeh

Introduction to JavaScript

9 min readNov 30, 2022

--

Press enter or click to view image in full size

WHY JAVASCRIPT?

WHAT IS JAVASCRIPT?

What can JavaScript do?

Lightweight

Cross-platform

Object-oriented

WHERE DOES JAVASCRIPT FIT IN?

NOUNS

ADJECTIVES

VERBS

Features of JavaScript

Javascript Attributes and properties

document.body.sayTagName = function() {
alert(this.tagName);
};

document.body.sayTagName(); // BODY (the value of "this" in the method is document.body)
Element.prototype.sayHi = function() {
alert(`Hello, I'm ${this.tagName}`);
};

document.documentElement.sayHi(); // Hello, I'm HTML
document.body.sayHi(); // Hello, I'm BODY

HTML attributes

<body id="test" something="non-standard">
<script>
alert(document.body.id); // test
// non-standard attribute does not yield a property
alert(document.body.something); // undefined
</script>
</body>
<body id="body" type="...">
<input id="input" type="text">
<script>
alert(input.type); // text
alert(body.type); // undefined: DOM property not created, because it's non-standard
</script>
</body>

Get Elvis Edeh’s stories in your inbox

Join Medium for free to get updates from this writer.

<body something="non-standard">
<script>
alert(document.body.getAttribute('something')); // non-standard
</script>
</body>
<body>
<div id="elem" about="Elephant"></div>

<script>
alert( elem.getAttribute('About') ); // (1) 'Elephant', reading

elem.setAttribute('Test', 123); // (2), writing

alert( elem.outerHTML ); // (3), see if the attribute is in HTML (yes)

for (let attr of elem.attributes) { // (4) list all
alert( `${attr.name} = ${attr.value}` );
}
</script>
</body>

Property-attribute synchronization

<input>

<script>
let input = document.querySelector('input');

// attribute => property
input.setAttribute('id', 'id');
alert(input.id); // id (updated)

// property => attribute
input.id = 'newId';
alert(input.getAttribute('id')); // newId (updated)
</script>
<input>

<script>
let input = document.querySelector('input');

// attribute => property
input.setAttribute('value', 'text');
alert(input.value); // text

// NOT property => attribute
input.value = 'newValue';
alert(input.getAttribute('value')); // text (not updated!)
</script>

DOM properties are typed

<input id="input" type="checkbox" checked> checkbox

<script>
alert(input.getAttribute('checked')); // the attribute value is: empty string
alert(input.checked); // the property value is: true
</script>
<div id="div" style="color:red;font-size:120%">Hello</div>

<script>
// string
alert(div.getAttribute('style')); // color:red;font-size:120%

// object
alert(div.style); // [object CSSStyleDeclaration]
alert(div.style.color); // red
</script>
<a id="a" href="#hello">link</a>
<script>
// attribute
alert(a.getAttribute('href')); // #hello

// property
alert(a.href ); // full URL in the form http://site.com/page#hello
</script>

Non-standard attributes, dataset

<!-- 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>
<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>
// a bit simpler than removing old/adding a new class
div.setAttribute('order-state', 'canceled');
<body data-about="Elephants">
<script>
alert(document.body.dataset.about); // Elephants
</script>
<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
alert(order.dataset.orderState); // new

// modify
order.dataset.orderState = "pending"; // (*)
</script>

Summary

JavaScript Advantages

JavaScript Limitation

SUMMARY

--

--