HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URLs added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
Any URL's added here will be added as <script>
s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
Search for and use JavaScript packages from npm here. By selecting a package, an import
statement will be added to the top of the JavaScript editor for this package.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<div class="container">
<h2>My Favorite Movies</h2>
<button type="button" id="new-movie" class="btn btn-sm btn-primary">Add New Movie</button>
<div id="new-movie-form">
<div id="status" class="error"> </div>
<form name="movieform" class="movie-form">
<div class="form-group">
<label for="title">Title:</label>
<input type="text" class="form-control" id="title">
</div>
<div class="form-group">
<label for="img">Image:</label>
<input type="text" class="form-control" id="img">
</div>
<div class="form-group">
<label for="rating">Rating:</label>
<input type="number" class="form-control" id="rating">
</div>
<!--<button type="button" id="save-new-movie" class="btn btn-primary" Save</button>
OR
-->
<input class="btn btn-sm btn-primary" type="submit" value="Save" id="save-new-movie" onClick=addMovie(movieform)>
</form>
</div>
<div id="table"> </div>
</div>
#new-movie-form {
display: none;
}
.visible {
display: block;
}
.error {
border-color:#ebccd1;
color:#a94442;
background-color:#f2dede;
}
.checked {
color: gray;
}
//Let's define our favorite movies
let movies = [
{
title: "The Notebook",
img:
"https://m.media-amazon.com/images/M/MV5BMTk3OTM5Njg5M15BMl5BanBnXkFtZTYwMzA0ODI3._V1_UX182_CR0,0,182,268_AL_.jpg",
rating: 5
},
{
title: "Our Times",
img:
"https://upload.wikimedia.org/wikipedia/en/thumb/f/f3/Our_Times%2C_Movie_Poster.jpg/220px-Our_Times%2C_Movie_Poster.jpg",
rating: 5
},
{
title: "Avengers",
img:
"https://m.media-amazon.com/images/M/MV5BMjMxNjY2MDU1OV5BMl5BanBnXkFtZTgwNzY1MTUwNTM@._V1_.jpg",
rating: 4
}
]
//=========================================================================== Start generate table structure
/*
<div id="table">
<table>
<thead>
<tr>
<th>Image</th>
<th>Title</th>
<th>Rating</th>
</tr>
</thead>
CONTENT TO BE GENERATED LATER ON
</table>
</div>
*/
/* Let's create variables to hold elements that will be used to create our table header*/
let movieTable = document.createElement("table")
let movieTableHead = document.createElement("thead")
let movieTableHeadRow = document.createElement("tr")
let movieTableHeaderHeading1 = document.createElement("th")
let movieTableHeaderHeading2 = document.createElement("th")
let movieTableHeaderHeading3 = document.createElement("th")
let movieTableHeaderHeading4 = document.createElement("th")
/* Set heading titles for each <th> element */
movieTableHeaderHeading1.innerHTML = "Image";
movieTableHeaderHeading2.innerHTML = "Title";
movieTableHeaderHeading3.innerHTML = "Rating";
movieTableHeaderHeading4.innerHTML = "";
/* Add bootstrap styling to table */
movieTable.setAttribute("class", "table table-striped");
/* appends <tr> to <thead> */
movieTableHead.appendChild(movieTableHeadRow);
/* appends <th> to <tr> */
movieTableHeadRow.appendChild(movieTableHeaderHeading1);
/* appends <th> to <tr> */
movieTableHeadRow.appendChild(movieTableHeaderHeading2);
/* appends <th> to <tr> */
movieTableHeadRow.appendChild(movieTableHeaderHeading3);
/* appends <th> to <tr> */
movieTableHeadRow.appendChild(movieTableHeaderHeading4);
/* appends <thead> to <table> */
movieTable.appendChild(movieTableHead);
/*add movie table to div with id "table"*/
document.getElementById("table").appendChild(movieTable);
//================================================================================== END generate table structure
//============================================================ Create <td> elements and call necessary utility functions to set <td> contents and attach events
function createMovieCellElementsAndAttachEvents(row, title, img, rating) {
let imgCell = document.createElement("td");
let titleCell = document.createElement("td");
let ratingCell = document.createElement("td");
let deleteCell = document.createElement("td");
let imageElement = document.createElement("img");
let deleteButton = document.createElement("button");
deleteButton.innerText = "X";
deleteButton.classList.add('btn');
deleteButton.classList.add('btn-sm');
deleteButton.classList.add('btn-danger');
addDeleteEvent(deleteButton);
deleteCell.appendChild(deleteButton);
deleteCell.classList.add('align-middle');
setCellContent(
titleCell,
imgCell,
ratingCell,
imageElement,
title,
img,
rating
);
addImageCellEvents(imgCell);
row.appendChild(imgCell);
row.appendChild(titleCell);
row.appendChild(ratingCell);
row.appendChild(deleteCell);
}
//================================= Style and add content to specified elements
function setCellContent(
titleCell,
imageCell,
ratingCell,
imageEl,
title,
image,
rating
) {
//set attributes for given image element "imageEl" GOOD
imageEl.src = image;
imageEl.alt = "image not found";
imageEl.width = 70;
imageEl.classList.add('img-thumbnail');
//set attributes for given "titleCell" <td> element
titleCell.classList.add('align-middle');
//set attributes for given "ratingCell" <td> element
/*
BAD
https://www.w3schools.com/jsref/met_element_setattribute.asp
because setAttribute will overwrite other CSS properties that may be specified in the style attribute
*/
ratingCell.setAttribute("class", "align-middle");
//add text to title and ratings cell element
titleCell.innerHTML = title;
//ratingCell.innerHTML = rating;
//OR
// add a <span> with a star based on how many ratings (stars are from font-awesome font library) */
/*for(let i = 0; i < rating; i++) {
let ratingSpan = document.createElement("span");
ratingSpan.classList.add("fa");
ratingSpan.classList.add("fa-star");
ratingSpan.classList.add("checked");
ratingCell.appendChild(ratingSpan);
} */
let ratingSpanThumbsUp = document.createElement("span");
ratingSpanThumbsUp.classList.add("fa");
ratingSpanThumbsUp.classList.add("fa-thumbs-up");
ratingSpanThumbsUp.style.padding = "10px";
let ratingSpanThumbsDown = document.createElement("span");
ratingSpanThumbsDown.classList.add("fa");
ratingSpanThumbsDown.classList.add("fa-thumbs-down");
let ratingSpanNumber = document.createElement("span");
ratingSpanNumber.style.cssText = "font-size:20px;";
ratingSpanNumber.innerHTML = rating;
ratingSpanNumber.style.padding = "10px";
increaseRating(ratingSpanThumbsUp);
decreaseRating(ratingSpanThumbsDown);
ratingCell.appendChild(ratingSpanThumbsUp);
ratingCell.appendChild(ratingSpanThumbsDown);
ratingCell.appendChild(ratingSpanNumber);
//add image to <td> element
imageCell.appendChild(imageEl);
}
//================================= toggle style
function hideShowElement(el) {
if (el.style.display === "none") {
el.style.display = "block";
} else {
el.style.display = "none";
}
}
/*************
EVENT HANDLERS
**************/
//================================= toggles div with id "new-movie-form"
document
.getElementById("new-movie")
.addEventListener("click", function() {
let el = document.getElementById("new-movie-form");
hideShowElement(el);
});
//================================= Add event to increase image size on mouseover
function addImageCellEvents(imgCell) {
imgCell.addEventListener("mouseover", function() {
imgCell.childNodes[0].width = 90;
});
imgCell.addEventListener("mouseout", function() {
imgCell.childNodes[0].width = 70;
});
};
//================================= Add event to delete row
function addDeleteEvent(button) {
button.addEventListener("click", function(event) {
console.log(event.target.parentNode.parentNode); //button's grandma is TR
movieTable.removeChild(event.target.parentNode.parentNode);
//console.log("movietabel :" + movieTable.childNodes[1].nodeName)
});
}
//================================= Add event to increase rating
function increaseRating(ratingSpanThumbsUp) {
ratingSpanThumbsUp.addEventListener("click", function(event) {
let numberRating = ratingSpanThumbsUp.nextSibling.nextSibling;
let newRating = Number(numberRating.innerHTML) + 1;
numberRating.innerHTML = newRating;
//alert(ratingSpanThumbsUp.nextSibling.nextSibling.innerHTML);
});
}
//================================= Add event to increase rating
function decreaseRating(ratingSpanThumbsDown) {
ratingSpanThumbsDown.addEventListener("click", function(event) {
let numberRating = ratingSpanThumbsDown.nextSibling;
let newRating = Number(numberRating.innerHTML) - 1;
numberRating.innerHTML = newRating;
});
}
/*************
Call functions to manipulate dom and trigger events
**************/
//================================= Let's create a <tr> and <td> elements from the movie objects we defined above
let body = document.createElement("tbody")
movieTable.appendChild(body);
movies.forEach(function(movie) {
let row = document.createElement("tr");
createMovieCellElementsAndAttachEvents(row, movie.title, movie.img, movie.rating)
body.appendChild(row);
});
function addMovie(movieform) {
event.preventDefault(); //If we don't specify this default action of page reload will happen
console.log("event: "+ event.target);
console.log("form: " + movieform[1].innerHTML);
console.log(document.forms.movieform[0].value);
// OR
console.log(movieform[0]);
let imgsrc = movieform[0].value;
let title = movieform[1].value;
let rating = movieform[2].value;
if (
imgsrc == "" ||
title == "" ||
rating == ""
) {
document.getElementById("status").innerHTML =
"OOPS! Please fill in all the fields.";
} else {
document.getElementById("status").innerHTML =
"";
let row = document.createElement("tr");
createMovieCellElementsAndAttachEvents(row, movieform[0].value, movieform[1].value, movieform[2].value)
//new row is inserted as first row
movieTable.insertBefore(row, movieTable.childNodes[0]);
//clear element content
movieform[0].value = "";
movieform[1].value = "";
movieform[2].value = "";
//hide form
hideShowElement(document.getElementById("new-movie-form"));
}
}
console.log("movie table: " + movieTable.rows[1].childNodes[2].innerHTML);
//console.log(movieTable.rows[1].cells.item(2).innerHTML);
//console.log(document.images[0]);
console.log("container: " +document.getElementsByClassName('container')[0].childNodes)
//parentNode, childNodes[#], firstChild, lastChild, nextSibling, previousSibling
const table = document.getElementsByTagName('table')[0]
console.log("Table's grandparent node: " + table.parentNode.parentNode) //<div class="container">
console.log("Table parent node: " + table.parentNode) //<div id="table">
console.log("Table child nodes: " + table.childNodes) //<thead> and <tbody>
console.log("NUm of child nodes: " + table.childNodes.length)
console.log("Table's first child': " + table.firstChild.nodeName) //<thead>
console.log("Table's last child': " + table.lastChild.nodeName) //<tbody>
console.log("Table's next sibling': " + table.nextSibling) //null
console.log("Table's previous sibling': " + table.previousSibling) //
table.childNodes.forEach(function(node){
console.log(node)
})
Also see: Tab Triggers