A slightly more robust and versatile version:
Code:
<!DOCTYPE html>
<html>
<head>
<title>Art Quiz</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
input[disabled] {
width: 1ex;
}
input[name="total"] {
width: 1.25em;
text-align: right;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
jQuery(function($){
var caseinsensitive = false, /* set to true to make answers case insensitive, false for case sensitive (unquoted) */
delimiter = ',' /* set separator character(s) for multiple answers in data-answer attributed (quoted) */
$f = $('form[name="quiz"]').submit(function(e){
e.preventDefault();
}), funcstring = caseinsensitive? 'toLowerCase' : 'valueOf', $inputs = $('input', $f);
$('input[data-answer]', $f).each(function(n, i){
var cn = 0, $i = $(i), an = $i.data('answer').split(delimiter);
$.each(an, function(i, a){
cn = Math.max(cn, a.length);
});
$i.attr('length', cn).data({$nextinput: $inputs.eq($inputs.index($i) + 1)});
}).on('change keyup mouseleave', function(){
var $i = $(this), an = $i.data('answer').split(delimiter), t = 0,
$next = $i.data('$nextinput').val(0);
$.each(an, function(i, a){
if($.trim(a[funcstring]()) === $.trim($i.val()[funcstring]())){$next.val(1);}
});
$('input[disabled]', $f).not('[name="total"]').each(function(n, i){
t += +i.value;
});
$('input[name="total"]', $f).val(t);
});
});
</script>
</head>
<body>
<form name="quiz">
Name this artist: <input type="text" data-answer="Vincent Van Gogh,Van Gogh"> <span>Pts: </span> <input disabled type="text"><br>
Name this painting: <input type="text" data-answer="Sunflowers"> <input disabled type="text"><br>
total: <input disabled type="text" name="total">
</form>
</body>
</html>
Should now work with earlier browsers than before (old IE 8 and less and FF before 1.5). Trims white space from the beginning and end of user supplied answers and defined answers. Defines elements more strictly allowing for other forms and/or similar inputs on the page outside of the 'quiz' form, and allows for a greater distribution of other elements in the quiz form if needed. Allows you to choose the separator used for multiple answers (can be more than one character, like two commas perhaps if single comma is used in an answer). I added case insensitive as an option before, but I think that was after you saw the code. It's included here as well.
NOTE: See edit timestamp below this for latest update time.
Bookmarks