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 Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
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.
<html lang="en">
<head>
<title>A contact manager, v4</title>
<meta charset="utf-8"/>
</head>
<body>
<form onsubmit="return formSubmitted();">
<fieldset>
<legend>Personal informations</legend>
<label>
Name :
<input type="text" id="name" required>
</label>
<label>
Email :
<input type="email" id="email" required>
</label>
<label>
City :
<input type="text" id="city">
</label>
<label>
Country :
<input type="text" id="country">
</label>
<br>
<button>Add new Contact</button>
</fieldset>
</form>
<h2>List of contacts</h2>
<div id="contacts"></div>
<p><button onclick="emptyList();">Empty</button>
<button onclick="cm.save();">Save</button>
<button onclick="loadList();">Load</button>
</p>
</body>
</html>
table {
margin-top: 20px;
}
table, tr, td {
border: 1px solid;
}
fieldset {
padding:10px;
border-radius:10px;
}
label {
display:inline-block;
margin-bottom:10px;
}
input {
float:right;
margin-right:70px;
width:150px;
}
input:invalid {
background-color:pink;
}
input:valid {
background-color:lightgreen;
}
"use strict";
window.onload= init;
// The contact manager as a global variable
let cm;
function init() {
// create an instance of the contact manager
cm = new ContactManager();
cm.addTestData();
cm.printContactsToConsole();
// Display contacts in a table
// Pass the id of the HTML element that will contain the table
cm.displayContactsAsATable("contacts");
}
function formSubmitted() {
// Get the values from input fields
let name = document.querySelector("#name");
let email = document.querySelector("#email");
let city = document.querySelector("#city");
let country = document.querySelector("#country");
let newContact = new Contact(name.value, email.value, city.value, country.value);
cm.add(newContact);
// Empty the input fields
name.value = "";
email.value = "";
city.value="";
country.value="";
// refresh the html table
cm.displayContactsAsATable("contacts");
// do not let your browser submit the form using HTTP
return false;
}
function emptyList() {
cm.empty();
cm.displayContactsAsATable("contacts");
}
function loadList() {
cm.load();
cm.displayContactsAsATable("contacts");
}
function removeRowContact(contactIndex){
cm.removeAtIndex(contactIndex);
cm.displayContactsAsATable("contacts");
}
class Contact {
constructor(name, email, city, country) {
this.name = name;
this.email = email;
this.city = city;
this.country = country;
}
}
class ContactManager {
constructor() {
// when we build the contact manager, it
// has an empty list of contacts
this.listOfContacts = [];
}
addTestData() {
var c1 = new Contact("Jimi Hendrix", "[email protected]","Steele","Tokelau");
var c2 = new Contact("Robert Fripp", "[email protected]","Woodland","Turkmenistan");
var c3 = new Contact("Angus Young", "[email protected]","Huguley","Martinique");
var c4 = new Contact("Arnold Schwarzenneger", "[email protected]","Meridianville","Niger");
this.add(c1);
this.add(c2);
this.add(c3);
this.add(c4);
// Let's sort the list of contacts by Name
this.sort();
}
// Will erase all contacts
empty() {
this.listOfContacts = [];
}
add(contact) {
this.listOfContacts.push(contact);
}
remove(contact) {
for(let i = 0; i < this.listOfContacts.length; i++) {
var c = this.listOfContacts[i];
if(c.email === contact.email) {
// remove the contact at index i
this.listOfContacts.splice(i, i);
// stop/exit the loop
break;
}
}
}
// remove contact given index
removeAtIndex(contactIndex) {
this.listOfContacts.splice(contactIndex, 1);
}
sort() {
// As our array contains objects, we need to pass as argument
// a method that can compare two contacts.
// we use for that a class method, similar to the distance(p1, p2)
// method we saw in the ES6 Point class in module 4
// We always call such methods using the name of the class followed
// by the dot operator
this.listOfContacts.sort(ContactManager.compareByName);
}
// class method for comparing two contacts by name
static compareByName(c1, c2) {
// JavaScript has builtin capabilities for comparing strings
// in alphabetical order
if (c1.name < c2.name)
return -1;
if (c1.name > c2.name)
return 1;
return 0;
}
printContactsToConsole() {
this.listOfContacts.forEach(function(c) {
console.log(c.name);
});
}
load() {
if(localStorage.contacts !== undefined) {
// the array of contacts is savec in JSON, let's convert
// it back to a reak JavaScript object.
this.listOfContacts = JSON.parse(localStorage.contacts);
}
}
save() {
// We can only save strings in local Storage. So, let's convert
// ou array of contacts to JSON
localStorage.contacts = JSON.stringify(this.listOfContacts);
}
displayContactsAsATable(idOfContainer) {
// empty the container that contains the results
let container = document.querySelector("#" + idOfContainer);
container.innerHTML = "";
if(this.listOfContacts.length === 0) {
container.innerHTML = "<p>No contacts to display!</p>";
// stop the execution of this method
return;
}
// creates and populate the table with users
var table = document.createElement("table");
var th = table.createTHead();
var thRow = th.insertRow();
thRow.innerHTML = "<td>Name</td><td>Email</td><td>City</td><td>Country</td><td></td>";
// create table body
var tBody = document.createElement ("tbody");
table.appendChild (tBody);
var count = 0; // variable to store the row index
// iterate on the array of users
// iterate on the array of users
this.listOfContacts.forEach(function(currentContact) {
// creates a row
var row = tBody.insertRow();
row.id = "row" + count;
var tData = row.insertCell();
tData.innerHTML = currentContact.name;
tData = row.insertCell();
tData.innerHTML = currentContact.email;
tData = row.insertCell();
tData.innerHTML = currentContact.city;
tData = row.insertCell();
tData.innerHTML = currentContact.country;
let trashbin = document.createElement("img");
trashbin.src = "http://i.imgur.com/yHyDPio.png";
trashbin.dataset.contactId = count;
//var _this = this;
if(trashbin.addEventListener) {
trashbin.addEventListener("click", function(){
var image = this;
removeRowContact(image.dataset.contactId);
});
}
tData = row.insertCell();
tData.appendChild(trashbin);
count++;
});
// adds the table to the div
container.appendChild(table);
}
}
Also see: Tab Triggers