I am looking to implement a feature in my quiz application where a green tick mark appears next to the question number for the correct answer and a red cross mark for the wrong answer

My HTML code here retrieves questions from a database and displays them based on the question number.

<h4>{{indexOfelement+1}}</h4>&nbsp;&nbsp;
  
 

In my CSS, I need to style the questions as follows: How can I achieve this? Each question has a corresponding answer in the database like so: question.questionAnswer. How do I write logic to display questions with the correct color coding? If a question is correct, it should be displayed in green with a check mark; if incorrect, it should be displayed in red with a cross mark. I need to create a logic to manage the colors for each question number. We have a list called reportAllQuestionDisplay that contains all questions and answers retrieved from an API.

Answer №1

The structure of the return data from the API is not shown, so I have created a mock data to illustrate the shape.

To see what I did, you can check out this demo:

Within the app.component.ts file, there is mock data available:

// Mock backend data.
  public resultViewModels: ResultViewModel[] = [
    {
      id: 1,
      question: 'How are you?',
      answer: 'I am great!',
      isCorrect: true,
    },
    {
      id: 2,
      question: 'Where are you?',
      answer: 'Somewhere',
      isCorrect: false,
    },
    {
      id: 3,
      question: 'What day is today?',
      answer: 'No sure',
      isCorrect: false,
    },
  ];

The returned shape of your API will differ, requiring adjustments. The id property indicates the answer number on the UI, while isCorrect is used for displaying Green vs. Red and toggling the icon visibility.

In the app.component.html

<div class="result-grid">
  <div
    *ngFor="let result of resultViewModels"
    class="result"
    [class.correct]="result.isCorrect"
    [class.incorrect]="!result.isCorrect"
  >
    <span>{{ result.id }}</span>
    <!-- Replace these with icon of your choice -->
    <span *ngIf="result.isCorrect">Check</span>
    <span *ngIf="!result.isCorrect">Cross</span>
  </div>
</div>

We utilize two functionalities here:

  1. [class.correct].
  2. *ngIf="result.correct"

The first one adds the class "correct" to the div if result.isCorrect is true.

The second one shows the "Check" icon when result.correct is true and hides it when result.correct is false.

Here is the app.component.css

.result-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(100px, 50px));
  gap: 16px;
}

.result {
  border: 1px solid gray;
}

/*
  used when [class.correct]="true"
*/
.result.correct {
  background-color: green;
}

/*
  used when [class.incorrect]="true"
*/
.result.incorrect {
  background-color: red;
}

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

Is there a tool available for monitoring server status with HTML/CSS

I am interested in a way to create an HTML or CSS code that can ping an address or IP every 10 minutes to determine if it is sending back a signal. If a signal is received, I want the status on my website to display as 'online'. In case there is ...

Fading effect of Bootstrap JS on reducing content size

My quest for a toggle collapse button led me to this helpful link: https://getbootstrap.com/docs/4.0/components/collapse/ I found what I needed, but encountered an issue with the transition; clicking on the button immediately reveals my div. I desire a slo ...

The overlay pop-up does not reposition on scroll movements

Within the image, there is a MatDialog Box positioned on top of another dialog box. Inside the MatDialog Box, there is a Component called MyNewDetailComponent. This component is accessed via Overlay with the scrollOptions set to reposition(). The issue ar ...

"Sequencing http.get requests in Angular 2 using

In my service, I have a series of http.get requests structured as follows: constructor(private http:Http) {} getDetails(sysID:string){ var details; this.http.get('https://blahURL').map(res => res.json().filter(f => f.id == another.id)[0] ...

There seems to be an issue with the pseudo-elements :before and :after

I have been attempting to apply a double border to my table using the "border" property for one, and :before and :after for the second. However, I am facing an issue as it is not working properly. I am relatively new to CSS and HTML and have tried my bes ...

Obtaining the value of an identification number from one service using the identification number from another service

I am currently working on an angular application that retrieves job information, including the customer's name. When storing this data in the jobs table, I make sure to include the customerId within the JobModel. Within my angular job component, I in ...

What's the best way to expand the right side of .li following a left offset of -20px

My nested list structure looks like this... https://i.sstatic.net/sggVx.png What you see is a recursive ul/li setup... <div class="family d-block"> <span class="pb-2"> <small class="text-muted">Family:</small><br> < ...

AngularJS seems to be ignoring CSS styles when displaying list items in the ng-repeat loop

After coming across a tutorial on creating round circles connected by a line in CSS, I decided to implement it myself. Interestingly, everything worked perfectly fine when I tested it with static HTML. However, when I tried to make it dynamic using Angular ...

Sleek dialog sliding animation with Svelte

I'm struggling with a svelte component that I have and I'm trying to implement a slide down animation when it closes. The slide up animation is functioning correctly, but for some reason the slide down animation is not working. Does anyone have a ...

Is it possible to create a dynamic <hr /> using Flexbox?

In the midst of working on my application, I managed to achieve a simple layout. However, there is one peculiar requirement that has been eluding me for the past 2 days and now I find myself in need of assistance. Here is the current HTML code I am workin ...

The lazy-loaded Angular module encountered an issue when attempting to access its child routes

I have been working on a dashboard app and currently, I've implemented two lazy loaded modules named AuthModule and AdminModule. The routing configuration in my app-routing-module.ts is as follows: const routes: Routes = [ { path: '&apo ...

Error: Unable to access the 'nom_gr' property of null - encountered in Chrome

<ion-col col-9 class="sildes"> <ion-slides slidesPerView="{{nbPerPage}}" spaceBetween="5"> <ion-slide *ngFor="let slide of lesClassrooms; let i = index" (click)="saveCurrentSlide(i)"> ...

Why does LESS keep prompting me with missing elements?

I am currently working on my first project using Less and have been following a tutorial closely. However, when I try to compile with lessc, I encounter the following error: ParseError: Unrecognised input. Possibly missing something in C:\Path\t ...

Utilizing nested endpoints in Angular resource with a Node.js server

In my Angular application, I have a resource called /cars with the endpoint defined as $resource('/cars/:carsId'); This allows me to retrieve all cars or a specific car. On the server side, I've implemented middleware to ensure that carsI ...

Eliminate the see-through effect from a logo positioned in a fading container

I created a div with image fading functionality using JavaScript and placed a static SVG logo on top of it. The issue I'm facing is that the logo appears somewhat transparent, allowing the image in the div to show through. Despite adjusting the z-inde ...

What steps can I take to prevent my menu items from overlapping in the mobile navigation menu?

I am currently working on creating a mobile menu, but I'm facing an issue where the menu items overlap when hovered over. Instead, I want the menu items to move downwards when hovered upon to prevent the text from overlapping. Below is the HTML code ...

When utilizing <number | null> or <number | undefined> within computed() or signals(), it may not function properly if the value is 0

I've encountered an issue while implementing signals and computed in my new Angular project. There's a computed value that holds an id, which is initially not set and will be assigned by user interaction. To handle this initial state, I attempte ...

Create a custom navigation bar using Bootstrap styling

I am currently attempting to create a unique Navigation Bar design using bootstrap https://i.sstatic.net/HDGFw.jpg Here is the code snippet I am working on: <div class="navbar-header"> <button type="button" class="navbar-toggle" data- ...

What steps can I take to ensure my header image adjusts properly for different screen sizes?

Currently, I am working on creating a website for a friend using an exquisite premium Wordpress theme. Despite having paid for the theme, the designer is unwilling to assist with customization. You can view the site in progress at www.zerocarbonfood.co.uk ...

The touch event doesn't seem to be functioning properly on the div element, but it works perfectly on the window element

I have a dilemma that's been puzzling me lately. I'm trying to append a touchevent to a div, but my current method doesn't seem to be working. Here's what I've tried: $("#superContainer").bind('touchstart', function(even ...