Click on the image to select a radio button option

Query

How can I display the appropriate dropdown when clicking on the image or checkbox, while hiding the radio button in the end? I suspect it may be a scoping problem as the options are not appearing correctly even when the radio button is checked.

View live code

I am working with a list of colors and sizes:

app.controller('Ctrl', function ($scope, $filter, $http) {
 $scope.productData = {
  "colors_and_sizes": {
    "data": {
      "Black": {
        "swatch_image": 'http://lorempixel.com/50/50',
        "sizes": "X Small, Small, Medium, Large, Xlarge, XX Large"
      },
      "Blue": {
        "swatch_image": 'http://lorempixel.com/50/50',
        "sizes": "X Small, Small, Medium, Large, Xlarge, XX Large"
      }
    }
  }
};

});

HTML: This is my form setup

<form ng-app="app" ng-controller="Ctrl">
    <div class="color-pick" ng-repeat="(key, val) in productData.colors_and_sizes.data">
        <input type="radio" ng-checked="selected" name="colors" ng-model="$parent.myColor" ng-value="key" />
        <img ng-click="selected = true" ng-src="{{val.swatch_image}}" alt="">{{key}} 
        <div class="size-pick" ng-show="$parent.myColor==key">
            <select ng-model="$parent.mySize" ng-options="size for size in val.sizes.split(',')"></select>
        </div>
    </div>
    myColor: {{myColor}}<br/>
    mySize: {{mySize}}
</form>

Answer №1

If you want to display the drop down menu based on the selected image, you can utilize the ng-click directive on the img tag to update the value. In your code snippet, $parent.myColor is used to store the value of the radio button.

<form ng-app="app" ng-controller="Ctrl">
    <div class="color-pick" ng-repeat="(key, val) in productData.colors_and_sizes.data">
        <input type="radio" name="colors" ng-model="$parent.myColor" ng-value="key" />
        <img ng-click="$parent.myColor = key" ng-src="{{val.swatch_image}}" alt="">{{key}} 
        <div class="size-pick" ng-show="$parent.myColor==key">
            <select ng-model="$parent.mySize" ng-options="size for size in val.sizes.split(',')"></select>
        </div>
    </div>
    myColor: {{myColor}}<br/>
    mySize: {{mySize}}
</form>

Give it a try here.

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

React Router Redux causing an infinite loop when navigating back

I recently encountered a strange issue with my React + Redux app. I am using React Router in combination with React Router Redux for routing, and I noticed some unexpected behavior. Clicking the back button once takes me from route 0 to -1 successfully. Ho ...

angularsjs state provider with multiple parameters

I am struggling to create a state provider that can handle multiple parameters. Is it possible to capture them as an object or array, or do I have to capture them as a string and then separate them? For example, this is my current provider: .state(' ...

Unlocking the power of setting global variables and functions in JavaScript

Within my language.js file, the following functions are defined: function setCookie(cookie) { var Days = 30; //this cookie will expire in 30 days var exp = new Date(); exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000); document.cookie = coo ...

The "ID" value for the row that was chosen is unable to be parsed with AJAX JQUERY

After populating an HTML table with data retrieved from a database using PHP, I encountered an issue while trying to fetch the correct ID using AJAX. Following a recommendation to use classes to build my jQuery code, I find myself stuck and unsure of what ...

What is the best way to send variables to a jQuery function?

I'm trying to create a form where I can hide unnecessary lines by toggling them with buttons using jQuery. I've managed to start working on a page, but I'm concerned about having to write a separate function for each button, which could beco ...

when the AJAX request has a specific condition

When making an AJAX request, I want to pass data only if a certain condition is met. The "data: testobj" line represents a JSON object that will return {"name":"tom"} $.ajax({ type: "POST", url: 'test.asp', data: testobj, succes ...

What benefits do Adobe Creative Cloud apps offer for developing interactive content in AEM?

My client recently transitioned to AEM for their website, and we are tasked with creating an interactive HTML5 'component' to be embedded on a page. While we have previously used custom HTML5 for components, integrating them into pages on the old ...

Tips for sending an object parameter using $http POST request and retrieving it in a Node.js service

I attempted to send a POST request where I included a parameter to the server, but encountered an error on the server side stating that the data was not found. Client-side JavaScript var postData= JSON.stringify({name:"Alex",age:"34"}); var deferred = $q ...

Activate a button by simulating a click event through a shortcut key with the help of a

I have incorporated the hotkeys plugin to enable shortcut functionality on my website. Currently, I am looking for a way to automatically trigger a button click when any shortcuts are pressed. hotkeys.add({ combo: 'alt+1', callback: function (da ...

Steps for assigning values to a JavaScript array using its indices

Question: Dynamically creating keys in javascript associative array Typically, we initialize an array like this: var ar = ['Hello', 'World']; To access its values, we use: alert(ar[0]); // Hello However, I am looking to assign ...

Is there a way to display the number of active users currently on a page using Pusher?

My goal is to create online classrooms with a maximum of two people in each room, allowing the teacher to know when both the teacher and student are connected. I attempted to track the number of users connected using Pusher's members.me function but ...

Enzyme 3 accesses the properties of the displayed DOM element

After retrieving the HTML with myDomNode.html(); <div aria-expanded="true"> {{someChildren}} </div> I need to verify that the aria-expanded attribute is set to true in my test. Previously, with enzyme 2.x and react 15.x, this code worked fin ...

Identifying 404 image status in AngularJS triggering by a button press

One question I have is about a button directive that I am working with: <button-data></button-data> The template for the button data directive looks like this: <div class="buttonDiv"> <a ng-show="!isSomthing" class="{{className}}" ...

Implementing jQuery getScript in a Ruby on Rails 3 application

After watching a railscast tutorial on loading records through ajax when a link is clicked, I implemented the following javascript: $(function() { $("#dash_container th a").live("click", function() { $.getScript(this.href); return false; }); } ...

Exploring NextJS with Typescript to utilize the getStaticProps method

I'm currently enrolled in a NextJS course and I am interested in using Typescript. While browsing through a GitHub discussion forum, I came across an issue that I don't quite understand. The first function provided below seems to be throwing an e ...

Using jQuery to retrieve the domain extension from a URL

Seeking assistance with extracting domain extensions from various URLs using jQuery. Uncertain how to account for all possible scenarios. Here are the parts of the URL that need to be extracted: https://www.amazon.**com**/dp/067144901X https://www.amazon. ...

Creating nested tabs using vanilla JavaScript, no need for jQuery

I am currently working on implementing tabs using a JavaScript function. The issue I am facing is that when I try to have tabs within one of the tabs, everything disappears every time I click on one of the inner tabs. This happens because the JavaScript fu ...

What is the best way to integrate this Google Dialogflow code into a TypeScript-based React application?

The integration of your chatbot into an HTML page using Google Dialogflow includes the following code snippet: <script src="https://www.gstatic.com/dialogflow-console/fast/messenger/bootstrap.js?v=1"></script> <df-messenger inten ...

Changing Image to Different File Type Using Angular

In my Angular Typescript project, I am currently working on modifying a method that uploads an image using the input element of type file. However, I no longer have an input element and instead have the image file stored in the assets folder of the project ...

How to extract the text content of an element using XPath in Selenium?

I have a setup that looks like this : <div class="content"> <h3 class="faq">Joocey</h3> Locked Bag 4005 <br> 6/78 William Street <br> Sydney NSW 2000 <br> Customer Ca ...