-
-
Save codingwithsara/b01eacb2f1b4abccc9dfffbe3944491d to your computer and use it in GitHub Desktop.
JavaScript Text Counter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="ja"> | |
<head> | |
<meta charset="utf-8"> | |
<title>テキストカウンター</title> | |
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> | |
<style> | |
.container { | |
width: 500px; | |
margin-top: 100px; | |
font-size: 18px; | |
} | |
#count1, #count2 { | |
color: grey; | |
} | |
</style> | |
</head> | |
<body> | |
<div class="container"> | |
<form> | |
<div class="form-group"> | |
<label>文字数カウントアップ</label> | |
<textarea class="form-control" rows="5" onkeyup="count_up(this);"></textarea> | |
<span id="count1" class="float-right">0</span> | |
</div> | |
<div class="form-group"> | |
<label>文字数カウントダウン</label> | |
<textarea class="form-control" rows="5" onkeyup="count_down(this);"></textarea> | |
<span id="count2" class="float-right">30</span> | |
</div> | |
</form> | |
</div> | |
<script> | |
function count_up(obj) { | |
document.getElementById('count1').innerHTML = obj.value.length; | |
} | |
function count_down(obj) { | |
var element = document.getElementById('count2'); | |
element.innerHTML = 30 - obj.value.length; | |
if (30 - obj.value.length < 0) { | |
element.style.color = 'red'; | |
} else { | |
element.style.color = 'grey'; | |
} | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment