Skip to content

Instantly share code, notes, and snippets.

@vielhuber
Created January 6, 2025 08:43
Show Gist options
  • Save vielhuber/891117e05a1adfc1cd886d78b10d2f8c to your computer and use it in GitHub Desktop.
Save vielhuber/891117e05a1adfc1cd886d78b10d2f8c to your computer and use it in GitHub Desktop.
input textarea auto capitalize uppercase
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="utf-8" />
<title>autocaps</title>
<script>
document.addEventListener('DOMContentLoaded', () => {
if( document.querySelectorAll('.autocaps').length > 0 ) {
document.querySelectorAll('.autocaps').forEach($el => {
$el.addEventListener('input', (e) => {
let p = $el.selectionStart;
$el.value = $el.value.toUpperCase();
$el.setSelectionRange(p, p);
});
});
}
});
</script>
</head>
<body>
<input class="autocaps" />
<textarea class="autocaps"></textarea>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment