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.
<h1>jQuery Table v2 Demo</h1>
<div id="table"></div>
<p> </p>
<div id="editableTable"></div>
<p> </p>
<button id="btnMakeTable">Make Table</button>
<button id="btnAppendRow">Append Row</button>
<button id="btnUpdateData">Update Data</button>
<button id="btnInsertRow">Insert Row</button>
<button id="btnAppendColumn">AppendColumn</button>
<button id="btnSetCellContentsAt">Set Contents At 1,2</button>
<button id="btnFindAndReplace">Find and Replace Text</button>
<button id="btnMakeEditableTable">Create Editable Table</button>
<button id="btnGetTextAt">Get Text At 2,2</button>
<button id="btnReset">Reset</button>
<br />
<br />
<br />
<br />
<hr />
<p>If you found this demo useful, please consider <a href="https://www.paypal.me/RobertGravelle/1" target="_blank">donating $1 dollar</a> (secure PayPal link) for a coffee or purchasing one of my songs from <a href="http://ax.itunes.apple.com/WebObjects/MZSearch.woa/wa/search?term=rob%20gravelle" target="_blank">iTunes.com</a> or <a href="http://www.amazon.com/s/ref=ntt_srch_drd_B001ES9TTK?ie=UTF8&field-keywords=Rob%20Gravelle&index=digital-music&search-type=ss" target="_blank">Amazon.com</a> for only 0.99 cents each. </p>
<p>Rob uses and recommends MochaHost, which provides <a href="http://www.mochahost.com/2425.html" target="new"> Web Hosting for as low as $1.95 per month, unlimited emails, and unlimited disk space!</a></p>
#table, #editableTable {
width: 800px;
}
.CSSTableGenerator {
margin:0px;padding:0px;
width:100%;
box-shadow: 10px 10px 5px #888888;
border:1px solid #000000;
-moz-border-radius-bottomleft:0px;
-webkit-border-bottom-left-radius:0px;
border-bottom-left-radius:0px;
-moz-border-radius-bottomright:0px;
-webkit-border-bottom-right-radius:0px;
border-bottom-right-radius:0px;
-moz-border-radius-topright:0px;
-webkit-border-top-right-radius:0px;
border-top-right-radius:0px;
-moz-border-radius-topleft:0px;
-webkit-border-top-left-radius:0px;
border-top-left-radius:0px;
}.CSSTableGenerator table{
border-collapse: collapse;
border-spacing: 0;
width:100%;
height:100%;
margin:0px;padding:0px;
}.CSSTableGenerator tr:last-child td:last-child {
-moz-border-radius-bottomright:0px;
-webkit-border-bottom-right-radius:0px;
border-bottom-right-radius:0px;
}
.CSSTableGenerator table tr:first-child td:first-child {
-moz-border-radius-topleft:0px;
-webkit-border-top-left-radius:0px;
border-top-left-radius:0px;
}
.CSSTableGenerator table tr:first-child td:last-child {
-moz-border-radius-topright:0px;
-webkit-border-top-right-radius:0px;
border-top-right-radius:0px;
}.CSSTableGenerator tr:last-child td:first-child{
-moz-border-radius-bottomleft:0px;
-webkit-border-bottom-left-radius:0px;
border-bottom-left-radius:0px;
}.CSSTableGenerator tr:hover td{
}
.CSSTableGenerator tr:nth-child(odd){ background-color:#aad4ff; }
.CSSTableGenerator tr:nth-child(even) { background-color:#ffffff; }.CSSTableGenerator td{
vertical-align:middle;
border:1px solid #000000;
border-width:0px 1px 1px 0px;
text-align:left;
padding:7px;
font-size:10px;
font-family:Arial;
font-weight:normal;
color:#000000;
}.CSSTableGenerator tr:last-child td{
border-width:0px 1px 0px 0px;
}.CSSTableGenerator tr td:last-child{
border-width:0px 0px 1px 0px;
}.CSSTableGenerator tr:last-child td:last-child{
border-width:0px 0px 0px 0px;
}
.CSSTableGenerator tr:first-child td{
background:-o-linear-gradient(bottom, #005fbf 5%, #005fbf 100%); background:-webkit-gradient( linear, left top, left bottom, color-stop(0.05, #005fbf), color-stop(1, #005fbf) );
background:-moz-linear-gradient( center top, #005fbf 5%, #005fbf 100% );
filter:progid:DXImageTransform.Microsoft.gradient(startColorstr="#005fbf", endColorstr="#005fbf"); background: -o-linear-gradient(top,#005fbf,005fbf);
background-color:#005fbf;
border:0px solid #000000;
text-align:center;
border-width:0px 0px 1px 1px;
font-size:14px;
font-family:Arial;
font-weight:bold;
color:#ffffff;
}
.CSSTableGenerator tr:first-child:hover td{
background:-o-linear-gradient(bottom, #005fbf 5%, #005fbf 100%); background:-webkit-gradient( linear, left top, left bottom, color-stop(0.05, #005fbf), color-stop(1, #005fbf) );
background:-moz-linear-gradient( center top, #005fbf 5%, #005fbf 100% );
filter:progid:DXImageTransform.Microsoft.gradient(startColorstr="#005fbf", endColorstr="#005fbf"); background: -o-linear-gradient(top,#005fbf,005fbf);
background-color:#005fbf;
}
.CSSTableGenerator tr:first-child td:first-child{
border-width:0px 0px 1px 0px;
}
.CSSTableGenerator tr:first-child td:last-child{
border-width:0px 0px 1px 1px;
}
/* START OF PLUGIN*********************************************
Author: Rob Gravelle
Version: 1.0
contact: rgconsulting@robgravelle.com
license: free to use, but donation of 1 dollar appreciated
via PayPal rob@robgravelle.com
July, 2016
About: This library makes generting and working with dynamic HTML tables easier.
It requires jQuery because it relies on jQuery's DOM manipulation functions.
To use it, include jQuery and this script in your page.
To create a new table, call the static library function:
$TBL.makeTable( container, data );
i.e.: var myTable = $TBL.makeTable( $('#tableDiv'), data );
$('#btnFindAndReplace').click(function() {
console.log( 'Replacing cells containing "UK" with "England" using defaults...' );
var matches = table.findAndReplace('UK', 'England');
console.log( 'Replaced ' + matches.length + ' instances.' );
console.log( 'Replacing first "New" with "Old" (case sensitive)' );
var options = {first: true,
caseInsensitive: false,
replaceMatchedTextOnly: true};
table.findAndReplace('New', 'Old', options);
}); The data is a two-dimentional array, where the first inner array is the table headers:
[["City 1", "City 2", "City 3"], //headers
["New York", "LA", "Seattle"], //1st row
["Paris", "Milan", "Rome"], //2nd row
["Pittsburg", "Wichita", "Boise"]] //etc...
All methods are accessible via the table instance.
i.e.: myTable.appendRow(["cell 1", "cell 2", "cell 3"]);
The $TBL(String selector) constructor also acts as a wrapper for table elements so that
they may access the library functions
i.e.:
var table = $TBL('#myTable');
table.appendRow(["cell 1", "cell 2", "cell 3"]);
If the selector matches more than one table, such as 'table'
then all tables will be enhanced to include the library functions
those that were created using the makeTable() function will be ignored.
Mathing tables may then be iterated over using .each:
tables.each(function() {
console.log( this.getDataAt(0,0) );
});
Instance functions include:
1. appendRow( Array rowData)
Appends a row at the end of a table and returns a reference to the new row
i.e.: var newRow = table.appendRow( ["Calgary", "Ottawa", "Yellowknife"] );
2. insertTableRow(Array rowData, int index)
Inserts a row at the given index (0-based) and returns a reference to the new row
i.e.: var newRow = table.insertTableRow( ["Calgary", "Ottawa", "Yellowknife"], 2 );
3. appendColumn(Array colData)
Appends a column at the right of a table and returns an array of cells
i.e.: var newCol = table.appendColumn( ["1", "2", "3"] );
4. insertTableColumn(Array colData)
Inserts a column at the specified (0-based) index and returns an array of cells
i.e.: var newCol = table.appendColumn( ["1", "2", "3"], 1 );
5. getData()
Returns all of the table vales as a 2-dimentional array
6. getDataAt(int rowIndex, int colIndex)
Fetches the value of the cell at the specified (0-based) index
i.e.: var val = table.getDataAt( 1, 1 );
7. getCellContentsAt(int rowIndex, int colIndex)
Fetches the child elements of the cell at the specified (0-based) index
i.e.: var val = table.getCellContentsAt( 1, 1 );
8. updateTableData(Array newData)
Updates the values of a table with the contents of the supplied 2-dimentional array.
i.e.: table.updateTableData( data );
*/
var $TBL = (function(){
var _$TBL = function(selector) {
var enchancedTables = $(selector).map(function() {
elt = $(this);
return elt.prop("tagName") === 'TABLE'
&& elt.appendRow === undefined
? $TBL.makeTable(elt)
: elt;
});
//console.log(enchancedTables);
return enchancedTables.length == 1 ? enchancedTables[0] : enchancedTables;
};
_$TBL.tableSupportsContentEditable = function() {
return $("<table contenteditable><tr><td></td></tr></table>")
.find('td').get(0).isContentEditable;
}
_$TBL.makeTable = function(container, data, options) {
var table = arguments.length < 2
? arguments[0]
: (function() {
var _table = $("<table/>"),
editable = options && options['editable'],
tableSupportsContentEditable = _$TBL.tableSupportsContentEditable();
if (editable && tableSupportsContentEditable)
_table.prop('contenteditable', true);
$.each(data,
function(rowIndex, r) {
var row = $("<tr/>");
$.each(r, function(colIndex, c) {
var cell = $("<t"+(rowIndex == 0 ? "h" : "d")+"/>");
if (editable && tableSupportsContentEditable === false) {
cell.append( $('<span/>').prop('contenteditable', true).text(c) );
}
else {
cell.text(c);
}
row.append(cell);
});
_table.append(row);
});
return container.append(_table);
})();
table.appendRow = function(rowData) {
//table.find('tr:last').index() also works
return this.insertTableRow( rowData, -1 );
};
table.insertTableRow = function(rowData, index) {
//console.log(table.find('tr:last').index());
var newRow = $('<tr/>').insertAfter( this.find('tr').eq(index) );
$(rowData).each(function(colIndex) {
newRow.append($('<td/>').text(this));
});
return newRow;
};
table.appendColumn = function(data) {
return this.insertTableColumn( data, this.find('tr > td:last').index() + 1 );
};
table.insertTableColumn = function(columnData, index) {
var newColumn = [],
colsCount = this.find('tr > td:last').index();
this.find("tr").each( function(rowIndex) {
var cell = $("<t"+(rowIndex == 0 ? "h" : "d")+"/>")
.text(columnData[rowIndex]);
newColumn.push(
index > colsCount
? cell.appendTo(this)
: cell.insertBefore( $(this).children().eq(index) )
);
});
return newColumn;
};
table.getData = function() {
var data = [];
this.find('tr').each(function (rowIndex, r) {
var cols = [];
$(this).find('th,td').each(function (colIndex, c) {
cols.push(c.textContent);
});
data.push(cols);
});
return data;
};
table.getDataAt = function(rowIndex, colIndex) {
return this.find('tr:eq(' + rowIndex + ') t' + (rowIndex == 0 ? "h" : "d")
+ ':eq(' + colIndex + ')').text();
};
table.getCellContentsAt = function(rowIndex, colIndex) {
return this.find('tr:eq(' + rowIndex + ') t' + (rowIndex == 0 ? "h" : "d")
+ ':eq(' + colIndex + ')').html();
};
table.updateTableData = function(newData) {
this.find('tr').each(function (rowIndex, r) {
$(this).find('td').each(function (colIndex, c) {
c.textContent = newData[rowIndex - 1][colIndex];
});
});
};
table.setCellContentsAt = function(rowIndex, colIndex, newContents) {
this.find('tr:eq(' + rowIndex + ') t' + (rowIndex == 0 ? "h" : "d")
+ ':eq(' + colIndex + ')').html('').append(newContents);
};
table.setCellTextAt = function(rowIndex, colIndex, newText) {
this.find('tr:eq(' + rowIndex + ') t' + (rowIndex == 0 ? "h" : "d")
+ ':eq(' + colIndex + ')').text(newText);
};
table.findAndReplace = function(search, replace, options) {
//debugger;
//set default options
var first = false,
exact = false,
caseSensitive = false,
replaceMatchedTextOnly = false;
//override option defaults
if (options) {
if (options['first']) first = !!options['first'];
if (options['exact']) exact = !!options['exact'];
if (options['caseSensitive'])
caseSensitive = !!options['caseSensitive'];
if (options['replaceMatchedTextOnly'])
replaceMatchedTextOnly = !!options['replaceMatchedTextOnly'];
}
var matches;
if (exact) {
if (!caseSensitive) {
matches = $("td").filter(function() {
return $(this).text().trim().toLowerCase() == search.toLowerCase();
});
}
else {
//escape single quotes
//console.log('Using :contains.');
matches = $("td:contains('" + search.replace(/'/g,'\\$1') + "')");
}
}
else {
matches = $("td").filter(function() {
var match = $(this).text().trim();
if (!caseSensitive) {
search = search.toLowerCase();
match = match.toLowerCase();
}
return match.indexOf(search) != -1;
});
}
if (first) matches = matches.first();
if (replaceMatchedTextOnly) replace = matches.text().replace(search, replace);
matches.text(replace);
return matches;
};
return table;
};
Array.prototype.mergeValues = function( a2, fn ) {
return $.map( this, function( a, i ) {
return new Array( $.map( a, function( val, j ) {
return fn.call( this, val, a2[i][j], i, j );
}) );
});
};
return _$TBL;
})();
//END OF PLUGIN*********************************************
function disableButtons() {
$("button").first().prop('disabled', false)
.siblings('button').prop('disabled', true);
}
$(document).ready(function() {
disableButtons();
var data = [["City 1", "City 2", "City 3" ], //headers
["New York", "LA", "Seattle"],
["Paris", "Milan", "Rome" ],
["Pittsburg", "Wichita", "Boise" ]],
table;
$('button').click(function(evt) {
$(evt.target).prop('disabled', true).next().prop('disabled', false);
});
$('#btnMakeTable').click(function() {
table = $TBL.makeTable($('#table'), data)
.addClass('CSSTableGenerator');
});
$('#btnAppendRow').click(function() {
table.appendRow(["Calgary", "Ottawa", "Yellowknife"]);
});
$('#btnUpdateData').click(function() {
var provinceStates = [["NY", "CA", "WA", "FL"],
["FR", "IT", "IT", "MD"],
["PA", "KS", "ID", "OR"],
["AL", "ON", "BC", "AL"]];
// console.log(getData(table));
var newData = table.getData().slice(1)
.mergeValues(provinceStates,
function(val1, val2, i1, i2) {
return val1 + ', ' + val2;
});
table.updateTableData(newData);
console.log( table.getDataAt(1, 1) );
});
$('#btnInsertRow').click(function() {
table.insertTableRow(["London, UK", "Manchester, UK", "Bath, UK"], 0);
});
$('#btnAppendColumn').click(function() {
table.appendColumn(["City X",
"Miami, FL",
"Washington DC, MA",
"Portland, OR",
"Anchorage, AL",
"Denver, CO"]);
});
$('#btnSetCellContentsAt').click(function() {
table.setCellContentsAt(1, 2, '<strong><label>new contents</label></strong>');
});
$('#btnFindAndReplace').click(function() {
console.log('Replacing all cell contents that contain "new" with "Old"...');
var matches = table.findAndReplace('new', 'Old', {caseSensitive:true});
console.log('Replaced instances: ' + matches.length + '.');
console.log('Replacing all cell contents that contain "New" or "new" with "Old"...');
matches = table.findAndReplace('New', 'Old');
console.log('Replaced instances: ' + matches.length + '.');
console.log('Replacing first cell contents that contain "UK" with "England"...');
var options = {
caseSensitive:true,
first:true
};
matches = table.findAndReplace('UK', 'England', options);
console.log('Replaced instances: ' + matches.length + '.');
console.log('Replacing all matched portions of cell contents that contain "UK" with "England"...');
options = {
caseSensitive:true,
replaceMatchedTextOnly:true
};
matches = table.findAndReplace('UK', 'England', options);
console.log('Replaced instances: ' + matches.length + '.');
});
$('#btnMakeEditableTable').click(function() {
table = $TBL.makeTable($('#editableTable'), data, {editable:true})
.addClass('CSSTableGenerator');
});
$('#btnGetTextAt').click(function() {
var data = table.getDataAt(2, 2);
console.log( data );
});
$('#btnReset').click(function() {
$('div.CSSTableGenerator').empty().removeAttr('class');
disableButtons();
});
});
Also see: Tab Triggers