07. Create Append π©βπ«π§βπ«
index.html
<!DOCTYPE html>
<html lang="en-gb">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Create and Append</title>
</head>
<body>
<script src="./script.js"></script>
</body>
</html>
script.js
// Stores user response in variable
var tagName = prompt("Please enter an HTML Tag (ex. h1, h2, p, div):", "enter tag");
if (tagName !== "h1" && tagName !== "h2" && tagName !== "p" && tagName !== "div") {
alert("please enter a valid tag");
} else {
// Creates element based on tag entered by user
var tag = document.createElement(tagName);
// Adds text content to created tag
tag.textContent = "This was made via prompts. It's a " + tagName + ".";
// Appends tag as child of document body
document.body.appendChild(tag);
}
var nextTag = confirm("Would you like to add another tag?");
if (nextTag === true) {
var secondTagName = prompt("Please enter another HTML Tag (ex. h1, h2, p, div):", "enter tag here");
if(secondTagName !== "h1" && secondTagName !== "h2" && secondTagName !== "p" && secondTagName !== "div") {
alert("please enter a valid tag");
} else {
var secondTag = document.createElement(secondTagName);
secondTag.textContent = "This is our second tag via prompts, it's a " + secondTagName + ".";
document.body.appendChild(secondTag);
}
}