Select radio button on clicking parent div and get its value in angularJS

Asmita

I want a DIV behaving like radio button and fetching its value in angularJS. My div looks like :

<div ng-repeat="address in multipleaddress" class="tileHover widget Text span4">
     <div class="radios" for="optionsRadios1{{address.id}}" id="{{address.id}}" ng-model="radiosship.shiphere">
        <h3 class="widget-title widget-title" id="shipaddr{{address.id}}">Address #{{$index + 1}}</h3>
        <span style="font-size:16px">{{address.firstname}} {{address.lastname}}</span>
        <p>{{address.shipping_address}}<br/>{{address.shipping_city}}, 
        <span ngif="{{address.country}} == 'CA'">{{address.shipping_province}}</span>
        <span ngif="{{address.country}} == 'US'">{{address.shipping_state}}</span>
        <span ngif="{{address.shipping_country}} != 'US' && {{address.shipping_country}} != 'CA'">{{address.otherregion}}</span> - {{address.pincode}},<br/> {{address.country}}</p>
       <input type="radio" name="shiphere" id="optionsRadios1{{address.id}}" ng-model="radiosship.shiphere" value="{{address.id}}"/>
   </div>

when I click on radios div, the radio button in that div should get selected and I want its value in $scope.radiosship.shiphere in angular. Any help?

Mohamed El-Sayed

I think the main issue here is that a DIV element can not use ng-model directive --assuming that you do not use a custom directive for that--.

so replace <div class="radios" for="optionsRadios1{{address.id}}" id="{{address.id}}" ng-model="radiosship.shiphere"> with <div class="radios" for="optionsRadios1{{address.id}}" id="{{address.id}}" ng-click="radiosship.shiphere = address.id; doSomething(address)">

also to bind the model value to the controller $scope rather than the components', you can initialize the radiosship variable in controller like so:

$scope.radiosship = {};

here is a working example

   angular.module('App', [])
   .controller('MainCtrl', function ($scope) {
     $scope.radiosship = {};
     $scope.multipleaddress = [
     {id: 1, firstname: 'John'},
     {id: 2, firstname: 'John'},
     {id: 3, firstname: 'John'},
     ];
   });
<!DOCTYPE html>
<html ng-app="App">
<head>
  <title>Radio</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
</head>
<body ng-controller="MainCtrl">
  <pre>{{radiosship | json}}</pre>
  <div ng-repeat="address in multipleaddress" class="tileHover widget Text span4">
     <div class="radios" for="optionsRadios1{{address.id}}" id="{{address.id}}" ng-click="radiosship.shiphere = address.id; fetchData(address)">
        <h3 class="widget-title widget-title" id="shipaddr{{address.id}}">Address #{{$index + 1}}</h3>
        <span style="font-size:16px">{{address.firstname}} {{address.lastname}}</span>
        <p>{{address.shipping_address}}<br/>{{address.shipping_city}}, 
        <span ng-if="address.country == 'CA'">{{address.shipping_province}}</span>
        <span ng-if="address.country == 'US'">{{address.shipping_state}}</span>
        <span ng-if="address.shipping_country != 'US' && address.shipping_country != 'CA'">{{address.otherregion}}</span> - {{address.pincode}},<br/> {{address.country}}</p>
       <input type="radio" name="shiphere" id="optionsRadios1{{address.id}}" ng-model="radiosship.shiphere" value="{{address.id}}"/>
   </div>
</body>
</html>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

AngularJS: Select radio button by clicking on outer element

From Dev

How to get value in a td by clicking its button

From Dev

How to get value in a td by clicking its button

From Dev

How to submit the value of Radio Button by clicking on the Answer/DIV

From Dev

How to get value of selected radio button in angularjs

From Dev

How to get radio button value in SweetAlert with AngularJS

From Dev

Clicking on title or radio button to select Fancytree node

From Dev

On clicking first radio button, automatically open radio button inside of that div

From Dev

Get radio button value

From Dev

How do i Get value of selected radio button in angularjs?

From Dev

Not able to get value of radio select setting dynmically in AngularJS

From Dev

Clicking button to add its text to another div

From Dev

AngularJS radio button will not select appropriate button from ng-model object value

From Dev

Clicking on a div and saving its value to use with PHP

From Dev

Radio button selection and its value in spring mvc

From Dev

get Value DataPicker and Radio Button

From Dev

Select options and radio button validation in angularjs

From Dev

Angularjs - Simple radio button NOT able select

From Dev

Clicking on a disabled radio button

From Dev

Clicking on radio button

From Dev

Clear all option select entries by clicking radio button

From Dev

how to use dojo to show/hide DIV by clicking radio button?

From Dev

Clicking radio button doesn't show other div

From Dev

how to use dojo to show/hide DIV by clicking radio button?

From Dev

Show hidden div to select a Radio button jquery

From Dev

Div not show/hide on radio button select

From Dev

How to get the value of a selected radio button and checkbox using its class in jQuery?

From Dev

Add/Remove parent div to another div by clicking button

From Dev

Add/Remove parent div to another div by clicking button

Related Related

  1. 1

    AngularJS: Select radio button by clicking on outer element

  2. 2

    How to get value in a td by clicking its button

  3. 3

    How to get value in a td by clicking its button

  4. 4

    How to submit the value of Radio Button by clicking on the Answer/DIV

  5. 5

    How to get value of selected radio button in angularjs

  6. 6

    How to get radio button value in SweetAlert with AngularJS

  7. 7

    Clicking on title or radio button to select Fancytree node

  8. 8

    On clicking first radio button, automatically open radio button inside of that div

  9. 9

    Get radio button value

  10. 10

    How do i Get value of selected radio button in angularjs?

  11. 11

    Not able to get value of radio select setting dynmically in AngularJS

  12. 12

    Clicking button to add its text to another div

  13. 13

    AngularJS radio button will not select appropriate button from ng-model object value

  14. 14

    Clicking on a div and saving its value to use with PHP

  15. 15

    Radio button selection and its value in spring mvc

  16. 16

    get Value DataPicker and Radio Button

  17. 17

    Select options and radio button validation in angularjs

  18. 18

    Angularjs - Simple radio button NOT able select

  19. 19

    Clicking on a disabled radio button

  20. 20

    Clicking on radio button

  21. 21

    Clear all option select entries by clicking radio button

  22. 22

    how to use dojo to show/hide DIV by clicking radio button?

  23. 23

    Clicking radio button doesn't show other div

  24. 24

    how to use dojo to show/hide DIV by clicking radio button?

  25. 25

    Show hidden div to select a Radio button jquery

  26. 26

    Div not show/hide on radio button select

  27. 27

    How to get the value of a selected radio button and checkbox using its class in jQuery?

  28. 28

    Add/Remove parent div to another div by clicking button

  29. 29

    Add/Remove parent div to another div by clicking button

HotTag

Archive