38

I have the following inputs and need their values:

<input type="hidden" ng-model="movie" value="harry_potter"/>
<input type="text" ng-model="year" value="2000"/>

What can I do to get the value of each input?

lukeocom
  • 3,243
  • 3
  • 18
  • 30
Bun Suwanparsert
  • 915
  • 2
  • 13
  • 32

2 Answers2

31

If you want to get values in Javascript on frontend, you can use the native way to do it by using :

document.getElementsByName("movie")[0].value;

Where "movie" is the name of your input <input type="text" name="movie">

If you want to get it on angular.js controller, you can use;

$scope.movie
Stefnotch
  • 511
  • 2
  • 13
  • 26
Hüseyin BABAL
  • 15,400
  • 4
  • 51
  • 73
  • 15
    There is no `document.getElementByName()` function. You're missing an '**s**' in the function name, and that function returns an array, so it would be `document.getElementsByName("movie")[0].value` (*assuming you only have 1 element with that name*). – baacke Jun 25 '15 at 22:34
  • Can we get a valid answer here that is actually best practice? $scope.movie / similiar $scope accessing does not work – Sam Alexander Nov 10 '16 at 16:39
  • I get property value does not exist on NodelistOf. – Norbert Norbertson Feb 27 '19 at 11:23
4

If your markup is bound to a controller, directive or anything else with a $scope:

console.log($scope.movie);

user2422960
  • 1,476
  • 6
  • 16
  • 28