JavaScript preprocessors can help make authoring JavaScript easier and more convenient. For instance, CoffeeScript can help prevent easy-to-make mistakes and offer a cleaner syntax and Babel can bring ECMAScript 6 features to browsers that only support ECMAScript 5.
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.
HTML Settings
Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.
<div class="info">
<h1>Custom <code>word-count</code> element</h1>
<p>This custome element counts the words typed into a textarea, input element or div with <code>contenteditable</code> attribute</p>
<p><b>BROWSER SUPPORT:</b>Works native in Chrome, Safari, and Opera.</p>
</div>
<div class="demo-area">
<div class="box">
<h2>Bound to <code><textarea></code></h2>
<textarea name="" id="my-box" cols="50" rows="10" placeholder="Enter some text"></textarea>
<word-count bind="#my-box" delay="100"></word-count>
</div>
<div class="box">
<h2>Bound to <code><input></code></h2>
<input type="text" name="" id="my-input"placeholder="Enter some text" />
<word-count bind="#my-input" delay="100"></word-count>
</div>
<div class="box">
<h2>Bound to <code><div contenteditable></code></h2>
<div contenteditable id="my-div">Enter some text</div>
<word-count bind="#my-div" delay="100" limit="5"></word-count>
</div>
</div>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font: 16px/1.5 Arial, Helvetica, sans-serif;
}
.info {
width: 100%;
text-align: center;
border-bottom: 1px solid #aaa;
padding: .5rem 0;
line-height: 1.8;
}
code {
background: #eee;
padding: 0 .125em;
}
.demo-area {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
}
.box {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
}
.box + .box {
margin-left: 4rem;
padding-left: 4rem;
border-left: 2px solid #aaa;
height: 100%;
}
textarea, input, div[contenteditable] {
width: 100%;
background: #ddd;
color: #333;
font-size: 1.3rem;
padding: .5rem;
border-radius: 3px 3px 0 0;
border: 1px solid #aaa;
border-bottom: none;
}
word-count {
display: block;
background: #ddd;
width: 100%;
border-radius: 0 0 3px 3px;
border: 1px solid #aaa;
border-top: 1px solid #ccc;
padding: .25rem;
text-align: right;
}
// Web Components are created by directly extending the HTMLElement
// which is the base class for all HTML Elements (<p>, <h1>, <div>, etc.)
class WordCount extends HTMLElement {
constructor() {
// Web Component setup
super();
this.attachShadow({mode: "open"})
// Initialize a base state
this.state = {
count: 0,
limitReached: false
}
// Get the element to observe
this._boundEl = document.querySelector(this.getAttribute('bind'))
// Init an empty timeput
this._timeout = null;
// Get the delay, defaults to 500ms
this._delay = parseInt(this.getAttribute('delay')) || 500;
// A optional character limit
this._limit = parseInt(this.getAttribute('limit')) || undefined;
console.log(this._limit)
// If there is a bound element, attach an event listener
if (this._boundEl) {
this._boundEl.addEventListener('keyup', (e) => {
this._timeout = setTimeout( () => {
let count = 0;
// Check if e.target.value is defined
if (!!e.target.value) {
count = e.target.value.split(' ').length;
} else {
// If there is no e.target.value try and read the textContent
count = e.target.textContent.split(' ').length;
}
// Update the word count
this.updateCount(count)
if(this._limit != undefined) {
this.updateLimitReached(count);
}
clearTimeout(this._timeout);
}, this._delay)
})
}
this.render(this.state);
}
CSS() {
return `
<style>
.limit-reached {
color: var(--word-count--limit-color, red);
}
</style>
`;
}
updateLimitReached(val) {
this.state.limitReached = parseInt(val) >= parseInt(this._limit)
this.render(this.state);
}
updateCount(val) {
// Update the state
this.state.count = val;
// Re-render the component
this.render(this.state)
}
render(state) {
let className = this.state.limitReached ? 'limit-reached' : '';
// Update the innerHTML of the Shadow DOM
this.shadowRoot.innerHTML = `${this.CSS()}<p class="${className}">You typed ${state.count} words</p>`;
}
}
// Register the custom element. All <word-count> HTML elements will be constructed
// by using the WordCount Class defined above.
window.customElements.define('word-count', WordCount);
Also see: Tab Triggers