Using Angular to Bind Checkbox Value in Typescript

I have a challenge of creating a quiz where a card is displayed with 4 questions structured like this:

<div class="col-md-6">
<div class="option" id="Answer1">
  <label class="Answer1">
    <input value= "Answer1"
    type="checkbox"
    (change) ="CheckAnswer(value)"/>{{question.answer1}}
  </label>
</div>
<div class="option" id="Answer2">
  <label class="Answer2">
    <input value= Answer2
    type="checkbox"
    (change) ="CheckAnswer(value)"/>{{question.answer2}}
  </label>
</div>
<div class="option" id="Answer3">
  <label class="Answer3">
    <input value= "Answer3"
    name="Answer3"
    type="checkbox"
    (change) ="CheckAnswer(value)"/>{{question.answer3}}
  </label>
</div>
<div class="option" id="Answer4">
  <label class="Answer4">
    <input value= "Answer4"
    type="checkbox"
    (change) ="CheckAnswer(value);"/>{{question.answer4}}
  </label>
</div>
</div>

Currently, my focus is on passing the value of the selected answer to my TS file using the CheckAnswer(value) method. However, when I try to console.log() the value in my method, it keeps showing as undefined.

 CheckAnswer(value : string) {
   
console.log(value);
}

Answer №1

When you see "undefined," it's likely because the value is not defined in the CheckAnswer(value) function within the HTML code.

To fix this issue, make sure to include the following format for all your input fields:

<input value= "Answer4"
    type="checkbox"
    (change)="CheckAnswer($event);"/>{{question.answer4}}

Also, here is the correct method to use:

CheckAnswer(event : any) {
   
console.log(event); // This should now display a value
console.log(event.target.value); // This should display the checkbox value
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Unable to view mobile version on HTML page / lack of responsiveness observed

<body> <nav class="navbar navbar transparent"> <div class="container-fluid logo"> <div class="navbar-header"> <a class="navbar-brand" href="#"> <img src="images/Logo.png" /> & ...

Tips on how to modify the session type in session callback within Next-auth while utilizing Typescript

With my typescript setup, my file named [...next-auth].tsx is structured as follows: import NextAuth, { Awaitable, Session, User } from "next-auth"; // import GithubProvider from "next-auth/providers/github"; import GoogleProvider from ...

What could be causing my website to automatically adjust the CSS height on its own?

My code looks like this: <li style = "height: 250px;"> <p>Foo</p></li> Despite my efforts, when I visit the website, the height doesn't seem to change. After inspecting the element in Chrome, I discovered that it's s ...

Enhancing user experience by implementing dynamic text box functionality that triggers ajax load for dropdown options using a combination

Here is the task I need help with, along with the code snippet: I want to enable the author select box when both the start and end dates are filled out. The author names should be fetched dynamically from the database based on the selected start and end ...

Replacing strings with file_get_contents in PHP is a common practice for retrieving

Currently, I am working on replacing a specific string within a file and so far everything seems to be going well. The file in question is an htm file formatted in html, but that shouldn't pose any issues. This is what my script looks like: $file = & ...

Creating Repeating nth-child Patterns with CSS

Currently grappling with a unique CSS challenge related to patterns. Here's what I'm aiming for: For LI 1-3 = green background For LI 4-6 = red background For LI 7-9 = blue background This sequence should repeat as follows: LI 10-12 = green ...

Tips on how to eliminate focus after choosing a radio button in Angular Material?

This is a snippet from my HTML template file, which includes two Angular Material radio buttons. My goal is to disable the focus on the rings quickly after selecting any radio button. <div id="login-form" class="vh-100 overflow-auto" ...

The 'connectedCallback' property is not found in the 'HTMLElement' type

After taking a break from my project for a year, I came back to find that certain code which used to work is now causing issues: interface HTMLElement { attributeChangedCallback(attributeName: string, oldValue: string, newValue: string): void; con ...

New Requirement for Angular Service: Subclass Constructor Must Be Provided or Unable to Resolve all Parameters for ClassName (?)

During a recent project, I encountered an issue while working on several services that all extend a base Service class. The base class requires a constructor parameter of HttpClient. When setting up the subclass with autocomplete, I noticed that their con ...

The issue with grunt-contrib-compass not functioning properly within npm

I recently installed npm and grunt, followed by installing grunt-contrib-compass. However, when I try to run grunt, I encounter the following error: Running “compass:dist” (compass) task Warning: not found: compass Use –force to continue. Aborted du ...

What causes the height and width properties in a div to remain unchanged even after using the zoom/scale CSS?

I'm trying to center a CSS spinner on the page, but I am struggling with making it happen. Even when scaled down by 50%, the height and width remain at 200px. <div class="center" style=""> <div class="uil-default-css" style="width: 200px ...

Integrate XML information into a webpage's table layout

I am currently working on integrating an XML file into my HTML and displaying it in a table. A snippet of the XML is provided below: <?xml version="1.0" encoding="UTF-8"?> <product category="DSLR" sub-category="Camera Bundles"> <id>0 ...

What is the best way to recreate this grid-like design using CSS?

By utilizing clever techniques such as margins and outlines, I was able to design the following. If you'd like to take a look at the live website, it can be found at https://i.sstatic.net/kl0an.jpg The CSS code that I used is as follows: body { ma ...

Achieving Content Alignment Using CSS

I successfully implemented a design on my HTML page that divides it into three sections vertically, with each section having a different background color - white, gray, and black. The backgrounds stretch the full width of the page regardless of screen reso ...

Exploring the use of global variables in React

Welcome to my React learning journey! I've encountered an issue while trying to access a global variable exposed by a browser extension that I'm using. Initially, I attempted to check for the availability of the variable in the componentDidMount ...

What is the best way to align the middle item of a flexbox using CSS?

I am attempting to present text divided into 3 divs - the middle div contains the highlighted portion of the text, while the first and last divs contain the text before and after the highlighted section. Both the first and last flex items have classes tha ...

Errors detected while attempting to install dependencies using pnpm: modules unspecified

I recently decided to experiment with using pnpm instead of npm for my new projects, but I've encountered an issue. Let's take my nuxt project as an example. First, I set up my project using the command: pnpx nuxi init my-project Then, I insta ...

In HTML, adjust column widths for multiple tables according to the widest column present in any of them

With Python, I am creating an HTML page that contains multiple tables with the same number of columns, all holding the same type of data. While the generated page is functional, I would like to improve readability by ensuring that all tables have the same ...

Error TS2339: The 'selectpicker' property is not found on the 'JQuery<HTMLElement>' type

Recently, I integrated the amazing bootstrap-select Successfully imported bootstrap-select into my project with the following: <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstra ...

Angular 2 - Graphic Representation Tool for Visualizing Workflows and Processes - Resource Center

Looking for a library that can meet specific requirements related to diagram creation and rendering. We have explored jsplumbtoolkit, mermaid, and gojs, but none of these fully satisfy our needs. For example, we need the ability to dynamically change con ...