Add code for select tag and textareas
Add code for select tag and textareas
diff --git a/js/game.js b/js/game.js
index 5c2445f..5d13fbc 100644
--- a/js/game.js
+++ b/js/game.js
@@ -494,7 +494,7 @@ class Location
* where the ck evaluates to false that x-present tags contents are
* omitted. In addition to usual HTML tags, a x-present tag can
* have x-speaker subtags. These llow one to present text from a speaker
- * in bubbles. An x-present tag may also involve x-input tags to
+ * in bubbles. An x-present tag may also involve input tags to
* receive/update values for x-objects or x-locations.
*/
async renderPresentation()
@@ -530,9 +530,9 @@ class Location
enableSavesAndInventory();
}
/**
- * Prepares input tags in the game so that they can bind to
- * game Object or Location fields by adding various Javascript
- * Event handlers. A input tag like:
+ * Prepares input, textareas, and select tags in the game so that they can
+ * bind to game Object or Location fields by adding various Javascript
+ * Event handlers. An input tag like:
* <input data-for="bob" name="name" >
* Binds with the name field of the bob game object (i.e., obj(bob).name).
* In the case above, as the default type of an input tag is text, this
@@ -556,26 +556,39 @@ class Location
content.innerHTML = interpolateVariables(content.originalHTML);
game.initializeGameNavListeners();
}
- let input_fields = content.querySelectorAll("input");
- for (const input_field of input_fields) {
- let target_object = null;
- let target_name = input_field.getAttribute("data-for");
- if (typeof target_name != "undefined") {
- if (game.objects[target_name]) {
- target_object = game.objects[target_name];
- } else if (game.locations[target_name]) {
- target_object = game.locations[target_name];
- }
- if (target_object) {
- let target_field = input_field.getAttribute("name");
- if (target_field) {
- input_field.value = target_object[target_field];
- if(!input_field.disabled) {
- input_field.addEventListener("input", (evt) =>
- {
- target_object[target_field] =
- input_field.value;
- });
+ let control_types = ["input", "textarea", "select"];
+ for (const control_type of control_types) {
+ let control_fields = content.querySelectorAll(control_type);
+ for (const control_field of control_fields) {
+ let target_object = null;
+ let target_name = control_field.getAttribute("data-for");
+ if (typeof target_name != "undefined") {
+ if (game.objects[target_name]) {
+ target_object = game.objects[target_name];
+ } else if (game.locations[target_name]) {
+ target_object = game.locations[target_name];
+ }
+ if (target_object) {
+ let target_field = control_field.getAttribute(
+ "name");
+ if (target_field) {
+ control_field.value =
+ target_object[target_field];
+ if(!control_field.disabled) {
+ if (control_type == "select") {
+ control_field.addEventListener("change",
+ (evt) => {
+ target_object[target_field] =
+ control_field.value;
+ });
+ } else {
+ control_field.addEventListener("input",
+ (evt) => {
+ target_object[target_field] =
+ control_field.value;
+ });
+ }
+ }
}
}
}