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="top-bar">
<ul class="nav-list">
<li><a href="#">Home</a></li>
<li><a href="#">Parts</a></li>
<li><a href="#">Search</a></li>
<li><a href="#">Help</a></li>
</ul>
<div id="search-box">
<input type="text" name="parts-search" placeholder="Mfg., Model, or Part Number" size=30>
</div>
</div>
<div id="search-controls">
<div id="result-sort">
Sort By
<select>
<option value="None">Popularity</option>
<option value="Acme">Price</option>
<option value="Bacme">Manufacturer</option>
</select>
</div>
<div id="filter-parts">
Search By
<select id="ddMfr">
<option value="All">Manufacturer</option>
<option value="Acme">Acme</option>
<option value="Bacme">Bacme</option>
<option value="Cacme">Cacme</option>
<option value="Dacme">Dacme</option>
</select>
<select id="ddModel">
<option value="All">Model</option>
<option value="TooCool">TooCool</option>
<option value="TooHot">TooHot</option>
<option value="JustRight">JustRight</option>
<option value="Coolerator">Coolerator</option>
</select>
<select>
<option value="All">Type</option>
<option value="Sniggler">Sniggler</option>
<option value="Wiggler">Wiggler</option>
<option value="Jiggler">Jiggler</option>
<option value="Tiggler">Tiggler</option>
</select>
</div>
<button type="button" id='go-btn'>Go</button>
</div>
<div id="parts-content" />
.top-bar,
#search-box,
.nav-list li,
.nav-list a {
background-color: #002D45;
font-family: Proxima Nova, helvetica neue, helvetica, sans-serif;
font-weight: bold;
font-size: 110%;
color: #ffffff;
}
.top-bar {
color: #FFFFFF;
height: 40px;
margin: 0px;
padding: 10px;
display: flex;
align-items: center;
}
.nav-list {
list-style-type: none;
padding: 0;
display: inline;
}
.nav-list li {
float: left;
}
.nav-list a {
display: inline;
padding: 8px;
text-decoration: none;
}
#search-box {
margin-left: auto;
margin-right: 0;
padding: 10px;
}
#search-controls {
background-color: #ffffff;
padding: 15px 15px;
font-family: 'Lucida Sans Unicode', 'Lucida Grande', sans-serif;
font-size: 70%;
display: flex;
align-items: center;
}
#filter-parts {
float: right;
margin-left: auto;
margin-right: 0;
padding-right: 5px;
}
.catalog-part {
margin: 2px;
padding: 0px 5px;
float: left;
border-style: solid;
border-color: #808285;
border-width: 1px;
width: 150px;
height: 200px;
line-height: 200px;
display: table-cell;
position: relative;
}
.catalog-part img {
height: auto;
vertical-align: middle;
width: 100%;
}
.overlay {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
height: 100%;
width: 100%;
opacity: 0;
transition: .5s ease;
background-color: #f4f4f4;
display: inline-table;
}
.catalog-part:hover .overlay {
opacity: .75;
}
p {
font-family: 'Lucida Sans Unicode', 'Lucida Grande', sans-serif;
line-height: 1;
overflow: scroll;
}
.catalog-part p {
vertical-align: middle;
text-align: center;
color: black;
display: table-cell;
vertical-align: middle;
font-size: 80%;
font-family: 'Lucida Sans Unicode', 'Lucida Grande', sans-serif;
}
//This is the URL of the Google sheet with a 'callback function' with a response we'll pare down to JSON. What's JSON? See: https://en.wikipedia.org/wiki/JSON
//If you want to see the URL of the sheet itself, copy this into your browser: https://docs.google.com/spreadsheets/d/1DA6htKy-Z-8QRkifkLbzlzG2r5YWOKE77JXEJNmq45g/.
sheetIndex = 0;
sheetsFileID = '1DA6htKy-Z-8QRkifkLbzlzG2r5YWOKE77JXEJNmq45g';
var SHEETSRESPONSE = `https://docs.google.com/spreadsheets/d/${sheetsFileID}/gviz/tq?tqx=out:json&tq&gid=${sheetIndex}`
//This waits until the baseline page is loaded and then gets the JSON feed, and as long as it doesn't error, calls the readdata function to start taking that feed and drawing the HVAC parts. The way it does this is through 'AJAX', which stands for 'Asynchronous JavaScript And XML'. This allows you to go and get data from a server/service (like Google Sheets) after the page has loaded.
/*This is probably your first time seeing options/settings supplied to a JS function with key:value pairs.
*/
document.addEventListener('DOMContentLoaded', function () {
fetch(SHEETSRESPONSE)
.then(response => response.text())
.then(textResponse => prepData(textResponse))
.then(partFeed => loadParts(partFeed))
.catch(error => console.error('Fetch error:', error));
});
function prepData(data) {
//response has a junk line at top (/*O_o*/), so we split it on the newline character and get the second row, [1]
data = data.split("\n");
//this is a kind of hacky solution to extracting the JSON body from the sheets response, using a regular expression to remove the stuff we don't want- OK for a proof of concept; and the HinH team is going to use Firebase for the 'production' version anyway
data = data[1].replace(/google.visualization.Query.setResponse\((.*)\);/, "$1");
if (isJson(data)) {
partfeed = JSON.parse(data);
return (partfeed);
} else {
console.log('it is not JSON- yarf');
}
}
function isJson(str) {
try {
JSON.parse(str);
} catch (e) {
return false;
}
return true;
}
function loadParts(partfeed) {
//these statements initialize the variables below, meaning that it puts them in the baseline state we want before we start doing stuff with them
const firstCol = 1;
/* This is a for loop that iterates through the rows from the Google Sheet, now JSON objects. What is a for loop? See: https://www.w3schools.com/js/js_loop_for.asp. See also the Codecademy module on Javascript: https://www.codecademy.com/learn/introduction-to-javascript.
//The JSON represents a single sheet in Google Sheets. The model is that the sheet has a table, the table has a series of rows, and those rows have values and then sometimes supplemental items. JS also has built-in facilities to index a JSON scheme where, for example, this statement:
…partfeed.table.rows[i].c[j].v;
based on the schema from Google Sheets and the JSON response it got, which is 'partfeed', is able to reference the value of cell in the jth column of the ith row of the table element. There's a pretty good written tutorial on JSON here, if you're interested:
https://www.w3schools.com/js/js_json_syntax.asp
*/
for (var i = 0; i < partfeed.table.rows.length; i++) {
//partfeed[i] indexes a given row in the array of data that has the JSON content. What is an array? See: https://www.w3schools.com/js/js_arrays.asp or check out the Codecademy tutorial above (module on arrays).
var row = [];
//console.log('we are on the ' + i + 'th row');
//this loop puts a row's worth of data, which is one part, into an array and passes it to another function that draws the div
for (var j = firstCol; j < partfeed.table.rows[i].c.length; j++) {
console.log('we are on the ' + j + 'th item');
val = partfeed.table.rows[i].c[j].v;
console.log(val);
//since we're iterating from the first column but we still want our arrays to start at 0, we do an offset of 1
row[j - 1] = val;
}
drawDiv(row);
}
}
//Basically, this function takes a rows-worth/parts-worth of data and maps the right parameters onto JQuery functions that draw the div.
function drawDiv(divData) {
if (divData == null) return null;
partNum = divData[0].trim();
imgURL = divData[1].trim();
mfr = divData[2].trim();
model = divData[3].trim();
type = divData[4].trim();
price = ("$" + String(divData[5]).trim());
//for bonus points, fix this so that the price always shows out to to decimal places (cents)
avail = divData[6].trim();
orders = String(divData[7]).trim();
//Write parts div
var newPartsDiv = document.createElement('div');
newPartsDiv.className = 'catalog-part';
newPartsDiv.setAttribute("mfr", mfr);
newPartsDiv.setAttribute("model", model);
var partImg = document.createElement('img');
partImg.setAttribute("src", ("https://" + imgURL));
newPartsDiv.appendChild(partImg);
var newOverlayDiv = document.createElement('div');
newOverlayDiv.className = 'overlay';
newOverlayDivP = document.createElement('p');
newOverlayDivP.innerHTML = (mfr + " " + partNum + "<br>" + orders + " Orders <br>" + price + " " + avail);
newOverlayDiv.appendChild(newOverlayDivP);
console.log(newOverlayDiv);
newPartsDiv.appendChild(newOverlayDiv);
console.log(newPartsDiv);
var theParent = document.getElementById("parts-content");
theParent.appendChild(newPartsDiv);
}
document.addEventListener("DOMContentLoaded", function(event) {
//find the "Go" button in the HTML body
const goButton = document.getElementById('go-btn');
//listen for the user clicking on it and if they do, call function 'goClick'
goButton.addEventListener('click', filterParts);
function filterParts() {
//Is the function working?
console.log('go!');
//get any values selected in the dropdown and make sure it's working
selMfr = document.getElementById('ddMfr').value
selModel = document.getElementById('ddModel').value
console.log("the selected mfr is " + selMfr + ", and the selected model is " + selModel);
//filter out items that don't match the dropdown- but only run if a filter's applied
//get all the parts- that is, items with class "catalog-part"
var parts = document.getElementsByClassName("catalog-part");
//go through each one
for (var i = 0; i < parts.length; i++) {
//reset to make show anything hidden
parts.item(i).style.display = "block";
//if the manufacturer doesn't match the dropdown, then hide it
if ((selMfr != parts.item(i).getAttribute('mfr')) && (selMfr != "All")) {
console.log("mfr diff");
parts.item(i).style.display = "none";
}
//...and same for model
if ((selModel != parts.item(i).getAttribute('model')) && (selModel != "All")) {
console.log("model diff");
parts.item(i).style.display = "none";
}
}
}
});
Also see: Tab Triggers