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">
<div class="col-12">
<div class="boxlist margin-top-30" id="training-box">
<div class="box-header bg-green-blue">
<h3 class="box-title">Simple Form Examples</h3>
</div>
<div class="panel-group" id="accordion">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#collapseOne">
<span id="oneSpan" class="glyphicon glyphicon-menu-right text-success"></span> The First Form
</a>
</h4>
</div>
<div id="collapseOne" class="panel-collapse collapse in">
<div class="panel-body">
<p>In this first example, we automatically generate a form from a simple JSON schema. The schema is read from <i><b>schema.js</b></i>, which directly assigns the schema to a variable called <i><b>dataModel</b></i>, that is used as input for the Metadata Editor Form. In addition, the form type is provided, which is in our case <i><b>CREATE</b></i>, which means we start with an empty form where all fields are editable. Thus, the parameter object looks as follows:
</p>
<pre>
var options = {
operation: "CREATE",
dataModel: this.dataModel
};</pre>
Using these parameters, we can now insert the form into the page. It takes the <i><b>options</b></i> object for parameterization and renders where the <i><b><form id="plain"></form></b></i> tags are located in this HTML file:
<pre>
$('#plain').metadataeditorForm(options, (value) => {
var resource = value;
JSON.parse(JSON.stringify(resource));
Console.log(resource);
});</pre>
Furthermore, the form received a callback function, which prints the resulting object at the console as soon as <i><b>CREATE</b></i> is clicked.</p>
<hr />
<div class="box-body">
<form id="plain"></form>
</div>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#collapseTwo">
<span id="twoSpan" class="glyphicon glyphicon-menu-right text-info"></span> A More Appealing Form
</a>
</h4>
</div>
<div id="collapseTwo" class="panel-collapse collapse">
<div class="panel-body">
<p>Now, it's time to work on a more user-friendly representation of the form. There are two things we are doing:
<ul>
<li>Apply a UI Schema</li>
<li>Overwrite the default submit button label</li>
</ul>
The UI Schema we include from the file <i><b>schema.js</b></i>, which assigns the schema to a variable called <i><b>uiSchema</b></i>. The resulting <i><b>options</b></i> object now looks as follows:
<pre>
var options = {
operation: "CREATE",
dataModel: this.dataModel
uiForm: this.uiSchema,
buttonTitle: "Submit"
};</pre>
In addition to providing the <i><b>uiForm</b></i> property you also see, that we provide <i><b>buttonTitle</b></i> which overwrites the default label of the submit button. Having this done, the form is rendered where the <i><b><form id="with-schema></form></b></i> tags are located using the following JavaScript call:
<pre>
$('#with-schema').metadataeditorForm(options, (value) => {
var resource = value;
JSON.parse(JSON.stringify(resource));
Console.debug(resource);
});</pre>
</p>
<hr />
<div class="box-body">
<form id="with-schema"></form>
</div>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#collapseThree">
<span id="threeSpan" class="glyphicon glyphicon-menu-right text-info"></span> A Form with Data
</a>
</h4>
</div>
<div id="collapseThree" class="panel-collapse collapse">
<div class="panel-body">
<p>Finally, we will learn how to add some data to show in the form. Therefore, we have a file <i><b>data.js</b></i> which contains a variable <i><b>resource</b></i> defining the input of our form. Furthermore, we have to change the options for parameterizing our form as follows:
<pre>
var options = {
operation: "READ",
dataModel: this.dataModel,
uiForm: this.uiSchema,
resource: this.resource,
buttonTitle: "Update"
};</pre>
As you can see, we changed to operation to <i><b>READ</b></i>, as operation <i><b>CREATE</b></i> does not accept any inputs. This input is provided by property <i><b>resource</b></i> pointing to <i><b>this.resource</b></i> which comes from <i><b>data.js</b></i>.
The result is rendered where the <i><b><form id="with-data"></form></b></i> tags are located using the following JavaScript call:
<pre>
$('#with-data').metadataeditorForm(options, () => {});</pre>
</p>
For <i><b>READ</b></i> forms no callbacks are supported as they are purely for visualization. Thus, the callback function remains empty in this example.
<hr />
<div class="box-body">
<form id="with-data"></form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
$("body").initializeModals();
var resource = {
"familyName": "Doe",
"givenName": "John",
"age": 42,
"gender": "Male"
};
var dataModel = {
"$schema": "https://json-schema.org/draft/2019-09/schema",
"$defs": {
"Gender": {
"type": "string",
"enum": ["Male", "Female", "Transgender", "Non-binary", "Not provided"]
}
},
"type": "object",
"properties": {
"familyName": {
"type": "string"
},
"givenName": {
"type": "string"
},
"age":{
"type": "integer"
},
"gender": {
"$ref": "#/$defs/Gender"
}
}
};
var uiSchema =
{
"type": "fieldset",
"legend": "Person",
"items": [
{"notitle": true, "placeholder": "Organization", "key":"givenName"},
{"notitle": true, "placeholder": "Family Name", "key": "familyName"},
{"notitle": true, "placeholder": "Age", "key": "age"},
{"notitle": true, "key": "gender"}
]
};
var view = {
template:
"<div>" +
'<input class="form-control typeahead" type="text" placeholder="<%=escape(node.placeholder)%>">' +
'<input id="ror-id-01" name="<%= node.name %>" value="<%= escape(node.value) %>" type="hidden">' +
"</div>",
inputfield: true,
array: false,
fieldtemplate: true,
getElement: function (el) {
return $(el).get(1);
},
onBeforeRender: function (data, node) {},
onInsert: function (evt, node) {
var ROR_API_URL = "https://api.ror.org/organizations?affiliation=";
$(".typeahead").typeahead(
{
hint: false,
highlight: true,
minLength: 3
},
{
limit: 50,
async: true,
source: function (query, processSync, processAsync) {
url = ROR_API_URL + encodeURIComponent(query);
return $.ajax({
url: url,
type: "GET",
dataType: "json",
success: function (json) {
orgs = json.items;
return processAsync(orgs);
}
});
},
templates: {
pending: [
'<div class="empty-message">',
"Fetching organizations list",
"</div>"
].join("\n"),
suggestion: function (data) {
return (
"<p><strong>" +
data.organization.name +
"</strong><br>" +
data.organization.types[0] +
", " +
data.organization.country.country_name +
"</p>"
);
}
},
display: function (data) {
return data.organization.name;
},
value: function (data) {
return data.organization.identifier;
}
}
);
$(".typeahead").bind("typeahead:select", function (ev, suggestion) {
$("#ror-id-01").val(suggestion.organization.id);
});
}
};
JSONForm.fieldTypes["ror-organization"] = view;
$("#accordion").on("show.bs.collapse", function (element) {
var id =
"#" + element.target.id.replace("collapse", "").toLowerCase() + "Span";
$(id).addClass("glyphicon-menu-down");
});
$("#accordion").on("hidden.bs.collapse", function (element) {
var id =
"#" + element.target.id.replace("collapse", "").toLowerCase() + "Span";
$(id).removeClass("glyphicon-menu-down");
});
resolveRefs(dataModel, dataModel.$defs);
var options = {
operation: "CREATE",
dataModel: this.dataModel
};
$("#plain").metadataeditorForm(options, (value) => {
var resource = value;
JSON.parse(JSON.stringify(resource));
console.log(resource);
alert(resource);
});
var options = {
operation: "CREATE",
dataModel: this.dataModel,
uiForm: this.uiSchema,
buttonTitle: "Submit"
};
$("#with-schema").metadataeditorForm(options, (value) => {
var resource = value;
JSON.parse(JSON.stringify(resource));
console.log(resource);
alert(resource);
});
var options = {
operation: "READ",
dataModel: this.dataModel,
uiForm: this.uiSchema,
resource: this.resource,
buttonTitle: "OK"
};
$("#with-data").metadataeditorForm(options, () => {});
Also see: Tab Triggers