|
| 1 | +/* |
| 2 | + * The DOM |
| 3 | + * |
| 4 | + * window - has browser information like viewport width |
| 5 | + * |
| 6 | + * document - has info about everything between html tags |
| 7 | + * |
| 8 | + * navigator - has info about the device: battery level, gps coordinates |
| 9 | + * |
| 10 | + * Use querySelector for classes and elements - it replaces getElementsByClassName... |
| 11 | + * Use getElementById for # |
| 12 | + */ |
| 13 | + |
| 14 | +const p = document.querySelector('p'); |
| 15 | +const imgs = document.querySelectorAll('.item img'); |
| 16 | +const item2 = document.querySelector('.item2'); |
| 17 | +const item2Image = item2.querySelector('img'); // look inside of .item2 |
| 18 | +const heading = document.querySelector('h2'); |
| 19 | + |
| 20 | +console.log(heading.textContent); |
| 21 | +console.log(heading.innerText); |
| 22 | + |
| 23 | +// set the textContent property of heading |
| 24 | +// heading.textContent = 'Josie is adorable'; |
| 25 | +// console.log(heading.textContent); |
| 26 | +console.log(heading.innerHTML); |
| 27 | +console.log(heading.outerHTML); |
| 28 | + |
| 29 | +// Append pizza emoji |
| 30 | +const pizzaList = document.querySelector('.pizza'); |
| 31 | +// pizzaList.textContent = `${pizzaList.textContent} 🍕`; |
| 32 | + |
| 33 | +// This is faster than textContent |
| 34 | +pizzaList.insertAdjacentText('beforeend', '🍕'); |
| 35 | + |
| 36 | +const pic = document.querySelector('.nice'); |
| 37 | +pic.classList.add('open'); |
| 38 | +pic.classList.remove('cool'); |
| 39 | +pic.classList.add('round'); |
| 40 | +console.log(pic.classList); |
| 41 | + |
| 42 | +// Add the class .round on click |
| 43 | +// classList gives you some nice utilities over addClass |
| 44 | +function toggleRound() { |
| 45 | + pic.classList.toggle('round'); |
| 46 | +} |
| 47 | + |
| 48 | +// Listen for a click then fire the function |
| 49 | +pic.addEventListener('click', toggleRound); |
| 50 | + |
| 51 | +/* |
| 52 | + * GET and SET attributes (don't do this) |
| 53 | + */ |
| 54 | +// SET image attributes |
| 55 | +// Add alt text to an image |
| 56 | +pic.setAttribute('alt', 'Adorable puppy'); |
| 57 | +pic.setAttribute('width', 500); |
| 58 | + |
| 59 | +// GET pic attribute values |
| 60 | +console.log(pic.getAttribute('alt')); |
| 61 | + |
| 62 | +// Get the width of the image after it loads |
| 63 | +// get works with naturalWidth, you can't set it though |
| 64 | +pic.addEventListener('load', function() { |
| 65 | + console.log(pic.naturalWidth); // width of actual pixels - run after image downloads |
| 66 | +}); |
| 67 | + |
| 68 | +/* |
| 69 | + * GET and SET data-attributes (reccomended) |
| 70 | + */ |
| 71 | +const custom = document.querySelector('.custom'); |
| 72 | +console.log(custom.dataset); |
| 73 | + |
| 74 | +custom.addEventListener('click', function() { |
| 75 | + alert(`Welcome ${custom.dataset.name} ${custom.dataset.last}`); |
| 76 | +}); |
0 commit comments