×
Clear all filters including search bar
Valeri Tandilashvili's JavaScript Notes
href
, text
of a link
JS//calling the function in window.onload to make sure the HTML is loaded
window.onload = function() {
var el = document.getElementsByTagName('a');
el[0].href= 'http://www.sololearn.com';
};
HTML<a href="http://www.example.com">Some link</a>
<br />
<a href="https://google.com">Google.com</a>
$
followed by curly brackets ${expression}
let name = 'George'
let age = 25
document.write(`My name is ${name} and I am ${age} years old!`)
replaceChild
(newNode, oldNode) method is used.
JSwindow.onload = function() {
let p = document.createElement("p");
p.innerHTML = "This is new";
// Replaces the first p with the new one
let parent = document.getElementById("demo");
parent.replaceChild(p, document.getElementById("p1"));
};
HTML<div id="demo">
<p id="p1">This is a paragraph.</p>
<p id="p2">This is another paragraph.</p>
</div>
Note: The code above creates a new paragraph element that replaces the existing p1
paragraphremoveChild
(node) method
JSwindow.onload = function() {
var parent = document.getElementById("demo");
parent.removeChild(document.getElementById("p1"));
};
HTML<div id="demo">
<p id="p1">This is a paragraph.</p>
<p id="p2">This is another paragraph.</p>
</div>
insertBefore
(node1, node2) inserts node1
as a child before node2
JSfunction myFunction() {
// Creates new list item
let newItem = document.createElement("li");
newItem.innerHTML = "Water";
// Puts list item into myList before "tea" item
let list = document.getElementById("myList");
list.insertBefore(newItem, document.getElementById("tea"));
}
HTML<ul id="myList">
<li>Coffee</li>
<li id="tea">Tea</li>
<li>Cocacola</li>
</ul>
<p>Click the button to insert an item to the list.</p>
<button onclick="myFunction()">Try it</button>
this
inside fn()
function refers to the global window
object.
That is why firstName
and lastName
attributes are not accessible using this
keywordlet john = {
firstName: 'John',
lastName: 'Doe',
driveCar() {
function fn () {
console.log(this.firstName + ' ' + this.lastName + ' is driving a car'); // "undefined undefined is driving a car"
console.log(this);
}
fn(); // fn.call(this);
console.log(this.firstName + ' ' + this.lastName + ' is driving a car');
console.log(this)
}
}
john.driveCar();
In order to fix the issue, one of the ways is to use call
function and pass the object that we want this
to refer to inside the fn()
functionfn.call(this);
Inside driveCar()
function this
refers to the john
object because it is called through john
objectjohn.driveCar();
document.addEventListener("scroll", setBG);
function setBG() {
if (!bgSet) {
var bgSet=1;
id('table_footer').style.background='url(/images/footer.jpg?tg)';
document.removeEventListener("scroll", setBG);
console.log('fired');
}
}
textarea
instead of using input
element function copy(part1, part2) {
// Creates textarea element
var textarea = document.createElement('textarea');
// Puts the text into the textarea with breaklines
textarea.value = part1 + "\n\n" + part2
// Adds the element to the DOM
document.body.appendChild(textarea);
// Selects the text that we want to copy
textarea.select();
// Copies the selected text clipboard
var result = document.execCommand('copy');
// Removes the element from the DOM because we no longer need it
document.body.removeChild(textarea);
return result;
}
hoisted(); // logs "foo"
function hoisted() {
console.log('foo');
}
Function expressions - anonymous functions are not hoisted:notHoisted(); // TypeError: notHoisted is not a function
var notHoisted = function() {
console.log('bar');
};
map
function calls getNames
function for each item of the array.
Each time the function getNames
returns only names from each objectlet myFavoritePlanets = [
{name: 'Earth', radius: 6371},
{name: 'Jupiter', radius: 69911},
{name: 'Neptune', radius: 24622, moons: ['Triton', 'Thalassa', 'Nereid', 'Proteus', 'Hippocamp', 'Naiad']}
]
let namesOnly = myFavoritePlanets.map(getNames);
function getNames(planet) {
return planet.name
}
console.log(namesOnly);