- How far did you get?
- What challenges did you face?
There's more we can do with the DOM, and it only gets more fun!
Up to this point, we've been manipulating things that are already present on the DOM. Let's create some.
Just use dot notation!
var myPTag = document.createElement("p");
myPTag.id = 'paragraphId';
myPTag.innerHTML = 'This is my amazing paragraph';
parent.appendChild(myPTag);
var h1 = document.createElement('h1');
h1.className = 'headingClass';
h1.textContent = 'The headline to an article';
parent.appendChild(h1);
var h1 = document.getElementsByTagName('h1')[0];
var bool = true;
//Add a class to an element
h1.classList.add('pull-left');
//Remove a class from an element
h1.classList.remove('pull-left');
//Toggle a class on an element
h1.classList.toggle('pull-right', [**optional**]bool);
//Replace one class with another, kind of like toggle
h1.classList.replace('pull-left', 'pull-right');
//Check if a class exists on an element
h1.classList.contains('pull-left');
var image = document.getElementsByTagName('img')[0];
//Check what the attributes are on the img element
image.getAttribute('alt');
//Remove an attribute from an element
//only works with inline attributes on the element itself
image.removeAttribute('class');
//Set an attribute on an element
//Caveat: if you're dealing with boolean values, you can just set the attribute itself, and no value.
image.setAttrbute('alt', 'Our Logo');
Let's try it out. Clone this repository. Open it in Atom and create this basic HTML page, using only the methods you've learned. HINT: Know that the head tag itself can be very finniky, so create that statically (in HTML) and make the rest dynamically (with JS). Use Bootstrap helper classes to shorten your workload.
Alright, let's put all this stuff we've learned together:
Write a program that prints on the page all numbers from 1 to 100. For multiples of three print “Fizz” instead of the number. For multiples of five print “Buzz” instead of the number. For numbers which are multiples of both three and five print “FizzBuzz”.
What you'll need:
Write a program that does the following: