CreateElement Exercises
CreateElement Exercises
1. Create a Heading
document.body.appendChild(heading);
2. Create a List
const ul = document.createElement('ul');
fruits.forEach(fruit => {
const li = document.createElement('li');
li.textContent = fruit;
ul.appendChild(li);
});
document.getElementById('fruit-list').appendChild(ul);
3. Create a Button
document.body.appendChild(button);
4. Create an Image
img.src = 'https://via.placeholder.com/150';
document.body.appendChild(img);
5. Create a Paragraph
document.querySelector('.content').appendChild(paragraph);
6. Create a Table
const th = document.createElement('th');
th.textContent = text;
headerRow.appendChild(th);
});
thead.appendChild(headerRow);
data.forEach(text => {
const td = document.createElement('td');
td.textContent = text;
row.appendChild(td);
});
tbody.appendChild(row);
table.appendChild(thead);
table.appendChild(tbody);
document.body.appendChild(table);
7. Create a Form
input.type = 'text';
input.placeholder = 'Name';
submitButton.type = 'submit';
submitButton.textContent = 'Submit';
form.appendChild(input);
form.appendChild(submitButton);
document.getElementById('form-container').appendChild(form);
8. Create a Footer
document.body.appendChild(footer);
article.appendChild(heading);
article.appendChild(paragraph);
section.appendChild(article);
document.body.appendChild(section);
links.forEach(linkText => {
link.textContent = linkText;
nav.appendChild(link);
});
document.querySelector('header').appendChild(nav);
11. Create a List of Checkboxes
hobbies.forEach(hobby => {
label.textContent = hobby;
checkbox.type = 'checkbox';
label.prepend(checkbox);
checkboxContainer.appendChild(label);
});
document.getElementById('hobby-
list').appendChild(checkboxContainer);
countries.forEach(country => {
const option = document.createElement('option');
option.value = country;
option.textContent = country;
select.appendChild(option);
});
document.querySelector('.country-selector').appendChild(select);
modal.style.position = 'fixed';
modal.style.top = '50%';
modal.style.left = '50%';
modal.style.padding = '20px';
closeButton.textContent = 'Close';
modal.appendChild(modalHeader);
modal.appendChild(closeButton);
document.body.appendChild(modal);
document.body.appendChild(openModalButton);
card.style.padding = '10px';
card.style.margin = '10px';
const cardImage = document.createElement('img');
cardImage.src = 'https://via.placeholder.com/100';
card.appendChild(cardImage);
card.appendChild(cardTitle);
card.appendChild(cardDescription);
document.getElementById('cards').appendChild(card);
progressBar.style.width = '100%';
progressBar.style.background = '#f3f3f3';
progressFill.style.height = '30px';
progressFill.style.background = '#4caf50';
progressBar.appendChild(progressFill);
document.body.appendChild(progressBar);
blockquote.appendChild(cite);
document.getElementById('quotes').appendChild(blockquote);
countdownDiv.textContent = countdownValue;
document.body.appendChild(countdownDiv);
const countdownInterval = setInterval(() => {
countdownValue--;
countdownDiv.textContent = countdownValue;
if (countdownValue <= 0) {
clearInterval(countdownInterval);
}, 1000);
socialMedia.forEach(platform => {
link.textContent = platform;
socialMediaContainer.appendChild(link);
});
document.querySelector('footer').appendChild(socialMediaContainer);
const images = [
'https://via.placeholder.com/150',
'https://via.placeholder.com/150/0000FF',
'https://via.placeholder.com/150/FF0000'
];
images.forEach(src => {
img.src = src;
img.style.margin = '10px';
gallery.appendChild(img);
});
document.body.appendChild(gallery);
20. Create a Welcome Message on Load
window.onload = () => {
document.body.appendChild(welcomeMessage);
};