1

I want it when I click on a text field, its value is changed by 2.

<input type='text' name='field[]' ng-model='field' ng-click='changeval()'>
<input type='text' name='field[]' ng-model='field' ng-click='changeval()'>
<input type='text' name='field[]' ng-model='field' ng-click='changeval()'>
<input type='text' name='field[]' ng-model='field' ng-click='changeval()'>

$scope.changeval=function(){
  //set value of input text for 2    
}

http://fiddle.jshell.net/0w36h8zm/

2
  • Why do you need to do on this way? Why the inputs must be the same name? Commented Dec 28, 2016 at 16:11
  • is an example, is anything that i need Commented Dec 28, 2016 at 16:19

2 Answers 2

1

Use Array and Index as parameter if you have a lot of fields

<input type='text' name='field[]' ng-model='field[0]' ng-click='changeval(0)'>
<input type='text' name='field[]' ng-model='field[1]' ng-click='changeval(1)'>
<input type='text' name='field[]' ng-model='field[2]' ng-click='changeval(2)'>
<input type='text' name='field[]' ng-model='field[3]' ng-click='changeval(3)'>

js

angular.module("myApp", ['ui.bootstrap'])
.controller("MyCtrl", function($scope, $modal) {

    $scope.field = [];
        $scope.changeval=function(index){
        //change the val for 2;
      $scope.field[index]=2;
    };


});

http://fiddle.jshell.net/0w36h8zm/9/

Sign up to request clarification or add additional context in comments.

Comments

0

If you want to change the Value in all the Text fields do this:

angular.module("myApp", ['ui.bootstrap'])
.controller("MyCtrl", function($scope, $modal) {
    $scope.changeval=function(){
    $scope.field=2;
}

});

If you want to change the value of the text field on which you click then do this :

     <input type='text' name='field[]' ng-model='field1' ng-click='changeval1()'>
      <input type='text' name='field[]' ng-model='field2' ng-click='changeval2()'>
           <input type='text' name='field[]' ng-model='field3' ng-click='changeval3()'>
                <input type='text' name='field[]' ng-model='field4' ng-click='changeval4()'>




     angular.module("myApp", ['ui.bootstrap'])
.controller("MyCtrl", function($scope, $modal) {
$scope.changeval1=function(){
$scope.field1=2;
 }

$scope.changeval2=function(){
$scope.field2=2;
}

$scope.changeval3=function(){
$scope.field3=2;
}


$scope.changeval4=function(){
$scope.field4=2;
}
 });

http://fiddle.jshell.net/0w36h8zm/3/

2 Comments

thank you, but i need have the same names for the inputs.... i can have n fields..
They all have the same name = 'field[]'. Only the model and the fucntion changed.

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.