Spaces:
Sleeping

Login / Ondex.html
Pezh's picture
Create Ondex.html
27cc61c verified
Raw History Blame Contribute Delete
1.79 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Checkbox List Flask App</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<input type="text" id="inputItem"/>
<button onclick="addItem()">Send</button>
<button onclick="listItems()">Check</button>
<div id="checkboxList"></div>
<script>
// Function to add item to list_1 on the server
function addItem() {
let item = $('#inputItem').val();
$.ajax({
url: '/add_item',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify({ 'item': item }),
success: function(response) {
if (response.success) {
alert(`Added: ${response.item}`);
} else {
alert('Failed to add item.');
}
}
});
}
// Function to list all items and create checkboxes
function listItems() {
$.ajax({
url: '/get_items',
type: 'GET',
success: function(data) {
let listDiv = $('#checkboxList');
listDiv.empty(); // Clear existing checkboxes
data.forEach(function(item, index) {
listDiv.append(`<input type="checkbox" id="item_${index}" />`);
listDiv.append(`<label for="item_${index}">${item}</label><br>`);
});
}
});
}
// Call listItems on load
$(document).ready(function() {
listItems();
});
</script>
</body>
</html>