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.
<header>
<h1>IndexedDB notes demo</h1>
</header>
<main>
<section class="note-display">
<h2>Notes</h2>
<ul>
</ul>
</section>
<section class="new-note">
<h2>Enter a new note</h2>
<form>
<div>
<label for="title">Note title</label>
<input id="title" type="text" required>
</div>
<div>
<label for="body">Note text</label>
<input id="body" type="text" required>
</div>
<div>
<button>Create new note</button>
</div>
</form>
</section>
</main>
<footer>
<p>Testing an excercise from
Mozilla.developer.org. Copyright nobody. Use the code as you like.</p>
</footer>
html {
font-family: sans-serif;
}
body {
margin: 0 auto;
max-width: 800px;
}
header, footer {
background-color: rgb(197, 199, 195);
color: white;
line-height: 100px;
padding: 0 20px;
}
.new-note, .note-display {
padding: 20px;
}
.new-note {
background: #ddd;
}
h1 {
margin: 0;
}
ul {
list-style-type: none;
}
div {
margin-bottom: 10px;
}
// Create needed constants Mark Webster wrote all this code
const list = document.querySelector('ul');
const titleInput = document.querySelector('#title');
const bodyInput = document.querySelector('#body');
const form = document.querySelector('form');
const submitBtn = document.querySelector('form button');
//console.log ('script got to line 7');
//create an instance of a db object for storage
let db;
//console.log ('script got to line 10');
window.onload = function(){
//open database if it doesnt already exist
/*open version 1 of notes database*/
let request = window.indexedDB.open('notes', 1);
//console.log ('script got to line 15');
//error handler
request.onerror = function() {
console.log('Database failed to open');
};
//console.log ('script got to line 20');
//onsuccess handler
request.onsuccess = function() {
console.log('Database opened successfully');
//store the opened database object in the db variable
db = request.result;
//console.log ('script got to line 27');
//run the displayData() to show notes already in db
displayData();
};//end request.onsuccess handler
// $$$$$ script can't get here console.log ('script got to line 31');
//onupgradeneeded handler
request.onugradeneeded = function(e) {
//grab a reference to opened db
let db = e.target.result;
/*create an objectStore to store our notes in (single table)
including an auto-incrementing key*/
let objectStore = db.createObjectStore('notes', { keyPath: 'id', autoIncrement:true});
//define what data items objectStore will contain
objectStore.createIndex('title', 'title', { unique: false });
objectStore.createIndex('body', 'body', { unique: false });
console.log('database setup complete');
};
//create an onsubmit handler to run the addData() function
form.onsubmit = addData;
function addData(e) {
//prevent Default, don't want to the form to submit in conventional way - causing refresh
e.preventDefault();
//grab values from form and store them in an object
let newItem = { title: titleInput.value, body: bodyInput.value };
//open a read/write db transaction, ready for adding database
let transaction = db.transaction(['notes'], 'readwrite');
//call an object store that's already been added to db
let objectStore = transaction.objectStore('notes');
//make a request to add our newItem object to the object storage
var request = objectStore.add(newItem);
request.onsuccess = function() {
//clear the form, ready for next entry
titleInput.value = '';
bodyInput.value = '';
};//end request.onsuccess function
//report on sucess of transaction
transaction.oncomplete = function(){
console.log('transaction completed: db mod finished');
//update display of data to show newly added item
displayData();
};
transaction.onerror = function(){
console.log('transaction not opened due to error');
};
}//end function addData;;;;;?
function displayData() {
/*empty the li contents to prevent duplicates
strips out the fistChild's until there are no more*/
while (list.firstChild) {//list = the <ul>
list.removeChild(list.firstChild);// remove <li>'s
}
//open our object store and get a cursor - which iterates
//thru all the different data items in store
let objectStore = db.transaction('notes').objectStore('notes');
objectStore.openCursor().onsuccess = function(e) {
//get a reference to the cursor....cursor????
let cursor = e.target.result;
//if there is still another data item to
//iterate thru keep running this code
if(cursor) {
//create a <li>, h3, p
let listItem = document.createElement('li');
let h3 = document.createElement('h3');
let para = document.createElement('p');
listItem.appendChild(h3);
listItem.appendChild(para);
//add <li> to inside of <ul> at bottom below other <li>'s
list.appendChild(listItem);
//put data from the cursor inside the h3 and para
h3.textContent = cursor.value.title;
para.textContent = cursor.value.body;
//store the ID of the data item inside an atribute
//on the list item so we know which list item.
//Will be useful later when we need to delete items
listItem.setAttribute('data-note-id', cursor.value.id);
//create a button andn place it in each <li>
let deleteBtn = document.createElement('button');
listItem.appendChild(deleteBtn);
deleteBtn.textContent = 'Delete';
//set event handler to
//trigger deleteItem() function to run
deleteBtn.onclick = deleteItem;
//iterate to next item in the cursor
cursor.continue();//this loops the if(true) block
} else {//when all records are looped thru this runs
//again, if list item is empty
if(!list.firstChild) {
let listItem = document.createElement('li');
listItem.textContent = 'No notes stored.';//$$$$they don't have ; here
list.appendChild(listItem);
}
//if there are no more cursor items to iterate through, say so
console.log('Notes all displayed');
}
};//end objectStore open cursor function
}//end function display data
function deleteItem(e) {
//retrieve name of task to delete, which is currently a string
//convert it to Number (IDB key type-sensitive)
let noteId = Number(e.target.parentNode.getAttribute('data-note-id'));
///open a db transaction and use above to find and delete
let transaction = db.transaction(['notes'], 'readwrite');
let objectStore = transaction.objectStore('notes');
let request = objectStore.delete(noteId);
//report that it's been deleteBtn
transaction.oncomplete = function() {
//delete the parent of button
//which is the <li>
e.target.parentNode.parentNode.removeChild(e.target.parentNode);
console.log('Note ' + noteId + ' deleted.');
//again, if list item is empty
if(!list.firstChild) {
let listItem = document.createElement('li');
listItem.textContent = 'No notes stored.';
list.appendChild(listItem);
}
};//end transaction.oncomplete function
}//end function deleteItem
};//end window.onload
Also see: Tab Triggers