Results: 62
Changing the
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>
Template literal syntax uses dollar sign
$
followed by curly brackets
${expression}
let name = 'George'
let age = 25

document.write(`My name is ${name} and I am ${age} years old!`)
To replace an HTML element, the element.
replaceChild
(newNode, oldNode) method is used. JS
window.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
paragraph
removeChild CODE
To remove an HTML element, we can select the parent of the element and use the
removeChild
(node) method JS
window.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 method CODE
element.
insertBefore
(node1, node2) inserts
node1
as a child before
node2
JS
function 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
keyword
let 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()
function
fn.call(this);
Inside
driveCar()
function
this
refers to the
john
object because it is called through
john
object
john.driveCar();
set footer background after only scroll down event
Sets background image only when a user scrolls down (not to load every image at first)
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');
    }
}
copy text to clipboard with break lines
If we need to copy text to clipboard with break lines, than we need to use
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;
}
Function declarations are hoisted. It means, we can use the function before we declare it
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 CODE
The
map
function calls
getNames
function for each item of the array. Each time the function
getNames
returns only names from each object
let 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);
Results: 62