You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
console.log(document.getElementById('name of the ID'))
gets the element with the given ID
usage examples:
varheadertitle=document.getElementById('name of the ID');console.log(headertitle);// gets the content within the given IDconsole.log(headertitle.textContent);// gets the text inside the given ID irrespective of the style applied (textContent)console.log(headertitle.innerText);// gets the text inside the given ID with respective of the style applied (innerText)headertitle.innerHTML='<h3>Hello</h3>';// adds h3(html) inside the given ID (innerHTML)headertitle.style.borderBottom='solid 3px #000';// used to modify the style for the given ID (style)
GETSELEMENTSBYCLASSNAME
Command
Description
console.log(document.getElementsByClassName('name of the class'))
gets the element with the given classname
usage examples:
varitems=document.getElementsByClassName('name of the class');console.log(items);// gets all the elements of the given classconsole.log(items[0]);// gets the first element of the given classitems[0].textContent='Hello';// adds text to the given classitems[0].style.fontWeight='bold';// changes the style of the given classitems.style.backgroundColor='green';// gives error// correct way for(vari=0;i<items.length;i++){items[i].style.backgroundColor='green';}
GETELEMENTSBYTAGNAME
Command
Description
console.log(document.getElementsByTagName('name of the tag'))
gets the element with the given tag name
usage examples:
varli=document.getElementsByClassName('name of the class');console.log(li);// gets all the elements of the given tagconsole.log(li[0]);// gets the first element of the given tagli[0].textContent='Hello';// adds text to the given tagli[0].style.fontWeight='bold';// changes the style of the given tagli.style.backgroundColor='green';// gives error// correct way for(vari=0;i<li.length;i++){li[i].style.backgroundColor='green';}
usage examples:
varheader=document.querySelector('#main-header');header.style.borderBottom='solid 4px #ccc';// adds style to given css selectorvarinput=document.querySelector('input');input.value='Hello World';varitem=document.querySelector('.list-group-item');item.style.color='red';// changes the color of first item in list-group-item classvarlastItem=document.querySelector('.list-group-item:last-child');lastItem.style.color='blue';// changes the color of the last item in list-group-item classvarsecondItem=document.querySelector('.last-group-item:nth-child(2)');secondItem.style.color='coral';// changes the color of the second item in list-group-item class
usage examples:
vartitles=document.querySelectorAll('.title');console.log(titles);// gets all elements with class titletitles[0].textContent='Hello';// changes the textcontent of first element with class titlevarodd=document.querySelectorAll('li:nth-child(odd)');// gets all the odd elements of the given css selectorvareven=document.querySelectorAll('li:nth-child(even)');// gets all the even elements of the given css selector for(vari=0;i<odd.length;i++){odd[i].style.backgroundColor='#f4f4f4';// changes the color of all odd elementseven[i].style.backgroundColor='#ccc';// changes the color of all even elements}
TRAVERSING THE DOM
varitemList=document.querySelector('#items');
•parentNode
usage examples:
console.log(itemList.parentNode);// gets the parent element of the itemList IDitemList.parentNode.style.backgroundColor='#f4f4f4';// changes the background color of parent element of itemList IDconsole.log(itemList.parentNode.parentNode);// gets the parent element of parent element of itemList ID
•parentElement
usage examples:
console.log(itemList.parentElement);// gets the parent element of the itemList IDitemList.parentElement.style.backgroundColor='#f4f4f4';// changes the background color of parent element of itemList IDconsole.log(itemList.parentElement.parentElement);// gets the parent element of parent element of itemList ID
•childNodes
usage examples:
console.log(itemList.childNodes);// gets all the child elements of itemList ID with line breaks
•children
usage examples:
console.log(itemList.children);// gets all the children elements of itemList IDconsole.log(itemList.children[1]);// gets the children element at index 1itemList.children[1].style.backgroundColor='yellow';
•firstChild
usage examples:
console.log(itemList.firstChild);// gets the first child element of itemList ID with line breaks
•firstElementChild
usage examples:
console.log(itemList.firstElementChild);// gets the first child element of itemList IDitemList.firstElementChild.textContent='hello';