Border will appear around the image once the corresponding radio button is selected

I am currently attempting to create a border or glowing effect around an image when the corresponding radio button is selected in my Angular program. Despite trying to modify certain properties of the image, I am facing difficulty as no changes are being applied to the image upon selecting the associated radio button.

This is the HTML code I have:

<div>
   <title>Quiz Page</title>
   <div class="quiz-container has-text-white">
       <h2>Quiz Program</h2>
       <span> {{timeLeft}} </span>
       <div class="card has-text-black">
           <form *ngIf="answer_form" [formGroup]="answer_form" (ngSubmit)="checkAnswers()" id="answer_form">
               <div *ngFor="let question of questions;index as idx">
                   <h3>Question {{idx+1}}:</h3>
                   <p>{{question.Question}}</p>
                   <div class="field">
                       <div class="control" *ngFor="let answer of answers">
                           <div class="fl" *ngIf="answer.QuestionID === question._id">
                               <label class="radio">
                                   <input type="radio" class="imgradio" value="{{answer._id}}" formControlName="{{question._id}}">
                                   <img class="max-size-image" src={{answer.Answer}} alt="">
                               </label>
                           </div>
                       </div>
                   </div>
               </div>
               <button class="button is-primary is-rounded" *ngIf="!boolTimesUp">Submit</button>
           </form>
           <p id="time-left">{{timeLeftDisplay}}</p>
           <p id="score">{{scoreDisplay}}</p>
       </div>
   </div>

Additionally, here is my CSS styling:

.quiz-container {
    background: rgb(4, 82, 137);
    background: linear-gradient(180deg, rgba(4, 82, 137, 1) 0%, rgba(22, 0, 84, 1) 50%, rgba(82, 1, 63, 1) 100%);
    margin: 0 auto;
    text-align: center;
    min-height: 100vh;
    max-height: fit-content;
}

h2 {
    font-size: 24px;
    margin-bottom: 10px;
}

p {
    font-size: 16px;
    margin-bottom: 10px;
}


#countdown {
    font-size: 24px;
    text-align: center;
}

#submit-btn {
    margin-top: 20px;
    padding: 10px;
    background-color: #4CAF50;
    color: white;
    border: none;
    border-radius: 5px;
    cursor: pointer;
}

.result {
    margin-top: 15px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}

.card {
    margin-left: 20%;
    margin-right: 20%;
    margin-top: 15px;
    margin-bottom: 5px;
    background-color: rgba(255, 255, 255, .15);
    backdrop-filter: blur(5px);
}

.quest {
    font-size: x-large;
    font-weight: bold;
}

input[type=radio]:checked ~ img {
    border: 2px solid red;
}

Despite applying this styling, the images do not change when selecting any of the radio buttons.

Answer №1

The sibling selector (~) chooses all following siblings, but it does not work across parent/child boundaries. In your HTML, input and img are not siblings; they are nested within different parent elements (label and div), so your CSS rule won't be applied.

To solve this, you can define a class in your CSS and utilize Angular's property binding to apply this class to the image when the radio button is selected. Let's name this class 'selected'.

CSS Code:

.selected {
    border: 2px solid red;
}

Then, in your HTML, add the class conditionally to the img tag:

<img class="max-size-image" 
     src={{answer.Answer}} 
     alt="" 
     [class.selected]="answer_form.value[question._id] === answer._id">

This links the class selected to the img tag when the condition within the brackets is true. The condition verifies if the selected value of the formControl (i.e., the chosen radio button) matches the answer._id. If it does, the class selected will be added to the img tag, highlighting your image with a border. I assume each answer has a unique ._id.

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

Tips for Implementing a Footer Component in ReactJS

My current setup includes: https://i.stack.imgur.com/2gvHk.png A navigation bar Nested content with a wizard and partial windows. A dynamic footer whose buttons and functionality need to change based on certain scenarios in the content. Currently, I ha ...

What is preventing me from subscribing once more to the same EventEmitter after I have unsubscribed?

I have implemented a subscription in the ngOnInit() method of an Angular2 component and then unsubscribed in ngOnDestroy(). However, after reinitializing the component, I encounter the following error: ObjectUnsubscribedError: object unsubscribed The cod ...

Tips on increasing the width of the 'select' option once the user decides to make a selection

Here's a question for you: I have a <select> box where I set the width to 120px: <select style="width: 120px"> <option>REALLY LONG TEXT, REALLY LONG TEXT, REALLY LONG TEXT</option> <option>ABC</option> < ...

React - Login page briefly appears while loading

Hello, I have built a React application that consists of a Login page, Loading page, and the main app itself. However, there is a small issue where after a user logs in, instead of smoothly transitioning to the Loading page until the data is loaded, the L ...

When utilizing useContext, the returned value may not always be what is anticipated, as it

Whenever I click the submit button in the consumer, instead of getting "useModal", I'm receiving "default block". Despite following all tutorials and guides online, I can't seem to get the expected value no matter what I do. This is a component ...

How can I make a field mandatory in a Yup schema depending on another field's value?

How can I create a validation rule that requires a title (input field) to be filled and also triggers the checkbox as required if the title is empty? I have attempted to follow Yup documentation for this purpose, but nothing seems to work. I've teste ...

How come the data from my post request in Python server gets stored in the database as "0"?

Currently, I am in the process of developing a full-stack application using react and flask. I am facing an issue where the form data that I am trying to send via a POST request from the client is being saved as "0" in the database. The react code function ...

What is the reason why the swiper feature is malfunctioning in my React-Vite-TS application?

I encountered an issue when trying to implement Swiper in my React-TS project. The error message reads as follows: SyntaxError: The requested module '/node_modules/.vite/deps/swiper.js?t=1708357087313&v=044557b7' does not provide an export na ...

Adjusting ToggleButton hues in react-bootstrap

I am working with a group of ToggleButtons in my app and I want to customize their background colors. Currently, all the buttons have a gradient defined by <.untoggled-button> class, with an added blue overlay for unselected buttons. However, I am fa ...

Customizing Magento Options: Exploring ways to tweak the design of product options

I'm attempting to stack the options vertically rather than side by side. I'm struggling to figure out which code I need to adjust, my brain seems to be failing me on this one. Should I make changes to the product-options-wrapper? ...

The Response Header for JWT in Angular2 Spring Boot is not appearing

I am encountering an issue with my Angular2 client, Angular-cli, Spring Boot 1.4.0, and jwt setup. When I sign in from my Angular2 client, I am unable to retrieve the jwt token. My security configuration is as follows: @Configuration @Order(SecurityPrope ...

The redirect feature for the authentication guard is ineffective when used on child routes

I am new to Angular and I'm working on an app where I handle my login service in the following way: export class AfterLoginService implements CanActivate { canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean | Observable& ...

Steps to transfer an array from an HTML page to Node.js and subsequently save it in a database

Looking for guidance on how to transfer a JavaScript array using Node.js to MySql. Seeking helpful videos or explanations to clarify the process. Thank you! ...

It's strange that I can easily access jQuery from the developer console, but for some reason I can't access it from

My script bundle contains jquery along with other elements, but when I try to use it from a component, I encounter an error message saying ERROR ReferenceError: $ is not defined. Strangely, when I type $('body') in the console, it works perfectly ...

Making an API request at regular intervals of x seconds

I am currently working on an Angular chat application where I need to fetch new messages at regular intervals. The time intervals for fetching new messages are as follows: 1. Every 5 seconds 2. Every 10 seconds 3. Every 20 seconds (if there are new message ...

Retrieving InnerHTML of a Rendered DOM Element in AngularJS

Can I retrieve the innerHTML code of a rendered element that contains an ng-repeat loop? Here is an example: <div id="container"> <div ng-repeat="e in ctrl.elements>{{e.name}}</div> </div> ...

When working with React.js, encountering the error message "Unexpected token export on export{default as

When attempting to import components into my newly installed app, I used the following syntax: import {Cards , Chart , CountryPicker} from '../components' I also created an index.js file with the following content: export {default as Cards} ...

Creating an Array in Angular 4

My goal is to populate an array with dynamic data. export class Test implements OnInit { private lineChart: Array<any>; } As I work on the code, I am dynamically generating some data and pushing it into the empty lineChart array. While this proc ...

Error: Unable to access 'map' property of an undefined variable in ReactJS

Hello everyone, I'm struggling to understand why I keep getting this error when trying to retrieve data from the Covid19 API. It returns an object that contains a Countries array, but when I try to map over the Countries, I encounter an error. However ...

The data type 'void | Observable<any>' cannot be assigned to the type 'ObservableInput<any>'. Specifically, the type 'void' cannot be assigned to 'ObservableInput<any>'

I encountered an error in my visual studio code: Argument of type '(query: string) => void | Observable' is not assignable to parameter of type '(value: string, index: number) => ObservableInput'. Type 'void | Observable& ...