Skip to content

Instantly share code, notes, and snippets.

@mgaffigan
Created November 22, 2019 03:18
Show Gist options
  • Save mgaffigan/ff6e24b577e437271def228ae13d809d to your computer and use it in GitHub Desktop.
Save mgaffigan/ff6e24b577e437271def228ae13d809d to your computer and use it in GitHub Desktop.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Hello, world!</title>
</head>
<body>
<h1>Hello, world!</h1>
<p>Example of a variable number of combo boxes</p>
<form>
<label>Enter the number of regions:</label>
<input type="number" id="regionCount" value="1" />
<input type="button" id="createRegions" value="Create" />
<ul id="regionList">
</ul>
</form>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script>
// Wait for the DOM to be ready
$(document).ready(function () {
// add an event handler for the "createRegions" button (onClick)
$('#createRegions').click(function () {
// see if we can get a number (coerce string to number with unary + operator)
var count = +$('#regionCount').val();
// add so many copies of the template
const selectTemplate = `<li>
<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
</li>`;
var fullList = '';
for (var i = 0; i < count; i++) {
fullList += selectTemplate;
}
$('#regionList').append(fullList);
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment