Skip to content

Commit 44f13db

Browse files
author
Aditya Satish Karwa
committed
Add real-time character counter for text box input
1 parent f60dcfa commit 44f13db

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

index.html

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ <h2>Have a play.</h2>
6262
<a href="#" class="btn btn-primary download-pdf">Download PDF</a>
6363
</div>
6464

65+
<div id="character-counter">Characters entered: <span>0</span></div>
66+
6567
<label class="checkbox-inline">
6668
<input type="checkbox" id="auto-refresh" checked="checked"> Auto refresh on changes?
6769
</label>
@@ -95,6 +97,22 @@ <h2>Have a play.</h2>
9597
});
9698
</script>
9799

100+
<script>
101+
$(document).ready(function() {
102+
const editor = ace.edit("editor");
103+
const $charCount = $('#character-counter span');
104+
const session = editor.getSession();
105+
106+
function updateCharCount() {
107+
const charCount = session.getValue().length;
108+
$charCount.text(charCount);
109+
}
110+
111+
editor.on('change', updateCharCount);
112+
$(window).on('load', updateCharCount);
113+
});
114+
</script>
115+
98116
<!-- Code editor -->
99117

100118
<script src="./examples/js/ace.js" type="text/javascript" charset="utf-8"></script>

package-lock.json

Lines changed: 0 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)