Skip to content

Instantly share code, notes, and snippets.

View jmlavoier's full-sized avatar
🏠
Working from home

João Lavoier jmlavoier

🏠
Working from home
View GitHub Profile
<body>
<div>
<div>
<div id="cont" class="content">
<p class="p1">Paragrafo 1</p>
<p class="p2">Paragrafo 2</p>
<p class="p3">
<input name="nome" value="teste" />
</p>
</div>
const content = document.querySelector('.content');
let p = document.querySelector('p');
let pList = document.querySelectorAll('p');
console.log(p); // <p class="p1">Paragrafo 1</p>
p = document.querySelector('.content > p.p3');
console.log(p); // <p class="p3">...</p>
console.log(pList); // [p.p1, p.p2, p.p3]
console.log(document.getElementById('cont')); // <div id="cont" class="content">...</div>
console.log(document.getElementsByClassName('content')); // [div#cont.content]
console.log(document.getElementsByTagName('div')); // [div, div, div#cont.content]
let p = document.querySelector('.content > p');
console.log(document.parentNode); // <div id="cont" class="content">...</div>
console.log(document.parentElement); // <div id="cont" class="content">...</div>
const p = document.querySelector('.p2');
console.log(p.previousElementSibling); // <p class="p1">Paragrafo 1</p>
console.log(p.nextElementSibling); // <p class="p3">...</p>
const p = document.querySelector('.content');
console.log(p.children); // [p.p1, p.p2, p.p3]
console.log(p.childNodes); // [text, p.p1, text, p.p2, text, p.p3, text]
const p = document.querySelector('.p1');
HTMLElement.prototype.parentsByTagName = function(tn) {
let arrayElements = [];
let currentEl = this;
while (currentEl) {
currentEl = currentEl.parentElement;
if (!currentEl) return arrayElements;
if (currentEl.tagName === tn) {
@jmlavoier
jmlavoier / 08-challenge-code2.js
Created June 27, 2017 21:01
08-challenge-code
const p = document.querySelector('.p1');
const parents = function (el) {
let arrayElements = [];
let currentEl = el;
return function findParentByTagName(tn) {
currentEl = currentEl.parentElement;
if (!currentEl) return arrayElements;
if (currentEl.tagName === tn) {
const div = document.createElement('DIV');
console.log(div); //<div></div>
const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
console.log(svg); //<svg></svg>