JSFiddle - React, Tailwind, and code Playground
HTML
<input type='text' class='letters[]'>
<input type='text' class='letters[]'>
<input type='text' class='letters[]'>
JavaScript
$(document).ready(function() {
//Focus the first field on page load
$(':input:enabled:visible:first').focus();
//Clear all fields on page load
$(':input').each(function() {
this.value = "";
});
});
//Clear field on focus
$('input').focus(function() {
this.value = "";
});
//Allow only alphabetical characters in the fields
$(':input').bind("keypress", function(event) {
if (event.charCode != 0) {
var regex = new RegExp("^[a-zA-Z]+$");
var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
if (!regex.test(key)) {
event.preventDefault();
return false;
}
$(this).next('input').focus();
}
});
//Enumerate click submit on [ENTER]-keypress
$(':input').keypress(function(e) {
if (e.which == 13) {
jQuery(this).blur();
jQuery('#submit').click();
}
});
//Submit form
$('#submit').click(function() {
//Show loading image while script is running
$("#response").html("<img src='../images/loader.gif'>");
//
function serealizeInputs(input) {
var array = [];
input.each(function() {
array.push($(this).val())
});
return array;
}
var letters = serealizeInputs($('.letters'));
$.post('loadwords.php', {
letters: letters
}, function(data) {
$("#response").html(data);
});
});