I'm currently working on retrieving user input values from a form. While vanilla JavaScript allows me to easily target elements and get their values using the .value method, I've encountered some challenges with TypeScript. Despite TypeScript being a superset of JavaScript, it seems to handle this task differently. Is there a specific approach to accessing input values in pure TypeScript, or should I consider using Angular or another framework?
Here is some TypeScript code I have been working on:
interface IUserFoodOptions {
food: string;
calories: number;
foodDate: any;
}
class Food implements IUserFoodOptions {
food: string;
calories: number;
foodDate: any;
// Array to store all entered calorie values and calculate total
caloriesArray: number[] = [];
// Constructor
constructor(food: string, calories: number, foodDate: any) {
this.food = food;
this.calories = calories;
this.foodDate = foodDate;
}
}
// Event listener for 'add food' button click
let addFood = document.getElementById("add-food-button").addEventListener("click", () => {
// Retrieve input values and store them in an array
let foodName = document.getElementById("food-name-val");
let foodCalories = document.getElementById("calories-val");
let dateVal = document.getElementById("date-val");
// Store these values in a list and display below for user interaction
});