i have two products each one of them have some images i want radio button for each…
- User
i have two products each one of them have some images i want radio button for each product to switch between images without one affect on other
- Assistant
product's images. You can achieve this by using JavaScript to display and hide images based on which radio button is selected. Here is some sample code:
HTML:
<div> <label> <input type="radio" name="product" value="product1" checked> Product 1 </label> <label> <input type="radio" name="product" value="product2"> Product 2 </label> </div> <div id="product1-images"> <img src="product1-image1.jpg"> <img src="product1-image2.jpg"> <img src="product1-image3.jpg"> </div> <div id="product2-images" style="display:none;"> <img src="product2-image1.jpg"> <img src="product2-image2.jpg"> <img src="product2-image3.jpg"> </div>JavaScript:
- User
in angular
- Assistant
In Angular, you can achieve the same functionality as in the previous answer by using property binding and ngIf directive. Here is an example:
HTML:
<div> <label> <input type="radio" name="product" value="product1" [(ngModel)]="selectedProduct"> Product 1 </label> <label> <input type="radio" name="product" value="product2" [(ngModel)]="selectedProduct"> Product 2 </label> </div> <div *ngIf="selectedProduct === 'product1'"> <img src="product1-image1.jpg"> <img src="product1-image2.jpg"> <img src="product1-image3.jpg"> </div> <div *ngIf="selectedProduct === 'product2'"> <img src="product2-image1.jpg"> <img src="product2-image2.jpg"> <img src="product2-image3.jpg"> </div>Angular:
import { Component } from '@angular/core'; @Component({ selector: 'app-product-images', templateUrl: './product-images.component.html', styleUrls: ['./product-images.component.css'] }) export class ProductImagesComponent { selectedProduct = 'product1'; }This code creates two radio buttons with
[(ngModel)]="selectedProduct"to bind the selected product to the property. It uses the ngIf directive to show or hide the image divs based on the value of . The TypeScript code initializes to 'product1'. You can customize the image sources and product values to match your own products and images.