1

I need to get the button id value using angularjs.I have written the code but i am getting the value as "undefined".So please suggest me the code which i need to use.

My code is:

 <button type="button"  class="btn btn-primary ole" data-toggle="tooltip"  id="buttonvalue" name="rdoResult"  value="SUN" ng-click="addvalue()"  ng-model="testDate23"  data-placement="left" data-original-title="this is a left tooltip">
      SUN
</button>

<button type="button"  ng-value="2"  class="btn btn-primary ole two" data-toggle="tooltip"  ng-click="addvalue()" 
 ng-model="testDate23" value="MON"  id="buttonvalue" data-placement="top" data-original-title="this is a top tooltip">
      MON
</button>

Script code:

$scope.addvalue = function() {
    var datevaluee=$scope.testDate23;
}
1
  • whatever the value of testDate23 will be before binding , it will be same in the method. What you want in $scope.testDate23 ? Commented Feb 10, 2016 at 4:07

5 Answers 5

2

You need to pass the $event in ng-click function like below..

   ng-click="addvalue($event)"

below will be the function implementation

    $scope.addvalue = function(element) {
      $scope.testDate23 = element.currentTarget.value; // this will return the value of the button
    console.log( $scope.testDate23)
   };
Sign up to request clarification or add additional context in comments.

Comments

1

you can try this.

    <button ng-click="addvalue(testDate23='SUN')">
     button
    </button>

or

   <button ng-click="addvalue(testDate23='MON')">
    btn2
   </button>

Find fiddle Fiddle example

Hope it will help.

1 Comment

you can use it with multiple buttons, with ng-repeat also.
0

Button is an input for submitting data, not setting data.

6 Comments

yes but when i click on that button i need to get the value
The only way i familiar with is via directive, scope.value = attrs.value
it is not working is there any to get the button value in angular
if you want the "value" remove the ng-value and vice versa. here is a plunkr that works plnkr.co/edit/0ZJsHVXULAV9JOnLRCH6?p=preview
I have seen the code but while loading the page the values is coming not when click on button please see that code
|
0

You need use directive.

Live example on jsfiddle.

var myApp = angular.module("myApp", []);

myApp.controller("myCtrl", function($scope) {
    $scope.getValue = function() {
      $scope.value = $scope.testDate23;
    }
  });
  
myApp.directive('buttonValue', function() {
    return {
      restrict: 'A',
      scope: {
       buttonValue:"="
      },
      link: function(scope, element, attr) {
        scope.$watch(function(){return attr.ngValue},function(newVal){
           scope.buttonValue = newVal;
        });
      }
    };
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myApp" ng-controller="myCtrl">
  <input ng-model="btnValue">
  <button ng-value="{{btnValue}}" ng-click="getValue()" button-value="testDate23">
    MON
  </button>
  <br>
  <pre>testDate23= {{testDate23}}</pre>
  <pre>value= {{value}}</pre>
</body>

Comments

0

If you want to set hardcoded value then use ng-init

<button type="button"  ng-init="value=2"  class="btn btn-primary ole two" data-toggle="tooltip"  ng-click="addvalue()" 
ng-model="testDate23" value="MON"  id="buttonvalue" data-placement="top" data-original-title="this is a top tooltip">
  MON
</button>

You can use value aywhere in your same template.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.