×
Clear all filters including search bar
Valeri Tandilashvili's JavaScript Notes
let myString = 'Some String'
The string variable has access to all string functions
document.write(myString.toUpperCase());
The result will be:SOME STRING
But if we let number variable to use the string function toUpperCase()
let myNumber = 55
console.log(myNumber.toUpperCase());
Then we will get the following error. This is because number type variable does not have access to string functionsUncaught TypeError: myNumber.toUpperCase is not a function
addEventListener
creates the specified event listener for the object.
In this case we add click
event for the div element with id div_element
without using anonymous function.
First we select the object that we want to create click
event listener tolet div = document.getElementById('div_element');
Then we create the actual event listener for the event without anonymous functiondiv.addEventListener('click', func);
function func() {
alert('You clicked me!');
}
The same example using anonymous functiondiv.addEventListener('click', function(){
alert('You clicked me!');
})
HTML part of the examples:<div id="div_element">Click Me</div>
Another method to add the event listener is to include onclick
attribute inside opening tag<div onclick="func()">Click Me</div>
boyfriend
inside cat
objectlet cat = {
name: 'Lucy',
age: 4,
meow() {
console.log('Meowwww');
},
boyfriend: {
name: 'Jack',
age: 5,
favoriteColors: ['Green', 'Yellow'],
meow() {
console.log('Meeoowwwwwwwwwwww');
},
}
}
Accessing nested object's attributes and methods:console.log(cat.boyfriend.name); // Jack
cat.boyfriend.meow(); // Meeoowwwwwwwwwwww
cat
combines attributes and behaviors of a cat in the following objectlet cat = {
age: 4,
name: 'Bella',
meow() {
console.log('Meowwww');
}
}
Instead of the object, we would have separated function and variables, which is not organized waylet catName = 'Bella';
let catAge = 4;
function meow() {
console.log('Meowwww');
}
green
background colordocument.body.style.backgroundColor = 'green';
Sets display
property to inline-block
for the element with id element_id
document.getElementById('element_id').style.display = 'inline-block';
console.log(document.title);
Updates the current web page titledocument.title = 'Getting started with JavaScript';
vanilla JavaScript
and calls the callback function
if the third parameter's type is function
function ajax(url, methodType, callback){
var xhr = new XMLHttpRequest();
xhr.open(methodType, url, true);
xhr.send();
xhr.onreadystatechange = function(){
if (xhr.readyState === 4 && xhr.status === 200){
if (typeof callback === "function") {
callback(xhr.responseText);
}
}
}
}
Example of calling the above methodajax(url, 'GET', function(resp) {
console.log(resp);
})
getElementsByClassName()
method returns a collection of all elements (as an array) in the document with the specified class name.
JSvar arr = document.getElementsByClassName("demo");
//accessing the second element
arr[1].innerHTML = "Hi";
HTML<div class="demo">1</div>
<div class="demo">2</div>
getElementById
method is used to select the element with id="demo"
and change its contentlet elem = document.getElementById("demo");
elem.innerHTML = "Hello World!";
document
object has methods that allow us to select the desired HTML element.
These three methods are the most commonly used for selecting HTML elements//finds element by id
document.getElementById(id)
//finds elements by class name
document.getElementsByClassName(name)
//finds elements by tag name
document.getElementsByTagName(name)
In the example below, the getElementById method is used to select the element with id="demo" and change its contentvar elem = document.getElementById("demo");
elem.innerHTML = "Hello World!";
Note: The example above assumes that the HTML contains an element with id="demo"
, for example <div id="demo"></div>