Actually this is Frontend JS :)
Getting an Element
document.getElementById('id');
document.getElementsbyClassName('className');
document.getElementsByTagName('tag');
document.querySelector('.class or #id');
document.querySelectorAll('.class or #id');
document....something....('').children[0].innerHTML; // This is the first list item
Creating an Element and/or its Attribute
var p = document.createElement('p'); // <p></p>
p.innerText = 'this is the created element'; // <p>this is the created element</p>
document.body.appendChild(p); // will add it at the end of its body
// Now creating its attribute
var att = document.createAttribute('id');
att.value = 'created';
p.setAttributeNode(att); // sets it for the p-tag above created
Write a code to getElementsByAttribute();
// Suggested Steps
// 1. Get all the elements in the DOM
// 2. Check if they have a particular attribute
// 3. Check that the attribute has the correct value
// The easy way:
function getElementsByAttribute(att, val){
return document.querySelectorAll('[' + att + '=' + val + ']');
} // document.querySelectorAll('[class=post]');
// The long and logical way
function getElementsByAttribute(attribute, value){
var all = document.getElementsByTagName('*'); // All the elements
var found = [];
for (var i = 0; i < all.length; i++) {
element = all[i];
if(all[i].getAttribute(atribute) === value){
found.push(all[i]);
}
}
return found;
}
Event Listeners
Some Common Events
- click : the user clicks
- resize : the user resizes the document
- keydown, keypress, keyup : the user interacts with the keyboard
- mouseover : mouse is moved over an element
- load : a resource has finished loading
CallBack for Event Listeners
// Syntax
window.addEventListener('load', function(event){
console.log('All resources finished loading!');
})
// Example-1
document.getElementsById('button-1 or someID').addEventListener('click', function(){
console.log('you clicked the button!');
})
// Example-2
document.getElementsById('button-2').addEventListener('mouseover', function(){
document.getElementsById('button-2').innerText = 'you hovered over me!';
})
// Example-3 Toggle theme by button click
// Make sure - you should have a css ready for each element as a child of 'theme2' class
// So now we have to add that class to our body tag, when the button is clicked
doucument.getElementById('theme-button').addEventListener('click', function(){
document.body.classList.toggle('theme2');
});
Create your own Custom Event
// Let's create an event called 'timeEvent' and have it call a function 'stateTime'
document.body.addEventListener('load', stateTime);
function stateTime(e) {
alert(e);
}
// Now, let's create a new custion event object
var myEvent = new CustomEvent('timeEvent', {
'detail' : new Date();
});
// Now go to Console and type in: document.body.dispatchEvent(myEvent);
Scope and 'this' variable
The call method
function greet(thing) {
console.log(this + " says greetings, " + thing);
}
greet.call("Cami", "earthlings")
//=> Cami says greetings, earthlings
// Another Example
var person = {
name: "Samantha",
greet: function(thing) {
console.log(this.name + " says greetings, " + thing);
}
}
person.greet.call(person, 'neighbor');
// Samantha says greetings, neighbor
The apply method
function greet(thing1, thing2) {
console.log(this + " says greetings, " + thing1);
console.log("But " + thing2 + " doesn't like " + this);
}
// How we do in 'call' method
greet.call('Samantha', 'Maya', 'Angelina');
// Samantha says greetings, Maya
// But Angelina doesn't like Samantha
// Same thing for 'apply' method
greet.apply('Samantha', ['Maya', 'Angelina']);
// Samantha says greetings, Maya
// But Angelina doesn't like Samantha
Difference between
call and
apply is that
- For call you need to count your parameters.
- But, for apply you have and array.
The bind method
function greet(thing1, thing2) {
console.log(this + " says greetings, " + thing1);
console.log("But " + thing2 + " doesn't like " + this);
}
var boundGreeting = greet.bind('Noah'); // the value for 'this' becomes 'Noah'
boundGreeting('Christian', 'Ben');
// Noah says greetings, Christian
// But Ben doesn't like Noah