JSFiddle - React, Tailwind, and code Playground
by lukempeters
HTML
<div class="wrapper">
<p>I think Luke is <input type="text" placeholder="" class="field size" /> inches long.</p>
<div class="msgBox"></div>
</div>
CSS
body {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 16px;
color: #4b4b4b;
background: #efefef;
}
.wrapper {
position: relative;
margin: 20px auto;
padding: 22px 24px;
width: 400px;
background: #ffffff;
}
p {
margin: 0;
}
.field {
position: relative;
margin: 0 3px;
padding: 4px 6px;
width: 2em;
font-size: 16px;
color: #4b4b4b;
text-align: center;
border: none;
outline: none;
box-shadow: inset 0 1px 3px rgba(0,0,0,0.2);
display: inline-block;
background: #ffffbb;
}
.msgBox {
position: relative;
margin: 20px auto 0 auto;
padding: 8px;
line-height: 1.4em;
color: #ffffff;
text-align: center;
text-shadow: rgba(0,0,0,0.2);
display: none;
background: red;
}
.msgBox.win {
background: green;
}
JavaScript
function checkLength(size)
{
switch(true)
{
case ((size >= 0) && (size <= 4)):
message('Wow, that\'s mean, try again.', false);
break;
case ((size >= 5) && (size <= 8)):
message('Do you think I\'m average?!', false);
break;
case ((size >= 9) && (size <= 11)):
message('I appreciate that, but guess bigger.', false);
break;
case (size == 12):
message('Correct!', true);
break;
case (size > 12):
message('Phew, I wish! Go smaller.', false);
break;
default:
message('Enter a number between 0 and 100.', false);
break;
}
}
function message(msg, win)
{
var $msgBox = $('.msgBox');
$msgBox.fadeOut(200, function() {
if(win === true) {
$msgBox.addClass('win');
} else {
$msgBox.removeClass('win');
}
$msgBox.text(msg).fadeIn(150);
});
}
$('.size').keypress(function(e) {
var $that = $(this);
if(e.which == 13) {
checkLength(parseInt($that.val()));
}
})