Adjusting input size to data-expected length

This commit is contained in:
Sergiusz Warga 2020-11-23 01:41:22 +01:00
parent 3c9a67d41b
commit 587fe18606
3 changed files with 15 additions and 2 deletions

View File

@ -4,6 +4,18 @@
<meta charset="UTF-8"> <meta charset="UTF-8">
<title>Familia Romana Pensum A</title> <title>Familia Romana Pensum A</title>
<script type="text/javascript"> <script type="text/javascript">
function adjust_size() {
const forms = document.querySelectorAll('form');
const form = forms[0];
Array.from(form.elements).forEach((input) => {
if (input.type=="text") {
input.setAttribute("size", input.dataset.expected.length);
}
});
}
function validate() { function validate() {
const forms = document.querySelectorAll('form'); const forms = document.querySelectorAll('form');
const form = forms[0]; const form = forms[0];
@ -29,5 +41,6 @@
<br> <br>
<input type="submit" value="Submit"> <input type="submit" value="Submit">
</form> </form>
<script type="text/javascript">adjust_size();</script>
</body> </body>
</html> </html>

View File

@ -1 +1 @@
Nīlus fluvi<input type="text" size="1" data-expected="us" required/> est. Nīlus et Rhēnus fluvi<input type="text" size="1" data-expected="ī" required/> <input type="text" size="1" data-expected="sunt" required/>, Crēta īnsul<input type="text" size="1" data-expected="a" required/> <input type="text" size="1" data-expected="est" required/>. Crēta et Rhodus īnsul<input type="text" size="1" data-expected="ae" required/> sunt. Brundisium oppid<input type="text" size="1" data-expected="um" required/> <input type="text" size="1" data-expected="est" required/>. Brundisium et Tūsculum oppid<input type="text" size="1" data-expected="a" required/> sunt. Nīlus fluvi<input type="text" data-expected="us" required/> est. Nīlus et Rhēnus fluvi<input type="text" data-expected="ī" required/> <input type="text" data-expected="sunt" required/>, Crēta īnsul<input type="text" data-expected="a" required/> <input type="text" data-expected="est" required/>. Crēta et Rhodus īnsul<input type="text" data-expected="ae" required/> sunt. Brundisium oppid<input type="text" data-expected="um" required/> <input type="text" data-expected="est" required/>. Brundisium et Tūsculum oppid<input type="text" data-expected="a" required/> sunt.

View File

@ -5,7 +5,7 @@ with open(sys.argv[1], 'r') as infile:
content = infile.read() content = infile.read()
# Replace _string_ with html # Replace _string_ with html
html_head = '<input type="text" size="1" data-expected="' html_head = '<input type="text" data-expected="'
html_tail = '" required/>' html_tail = '" required/>'
content = re.sub(r'_([a-zA-Z\-]+)_', html_head+r'\1'+html_tail, content) content = re.sub(r'_([a-zA-Z\-]+)_', html_head+r'\1'+html_tail, content)