Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Get HTML element using class name or Id in AngularJS

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 11.9k
    Comment on it

    Hi All,

    In this blog we will see how easy is to get HTML element in AngularJS. We many a times stuck in situation where we need to get HTML element that are not bind to AngularJS Model. Here is quick and easiest way of doing that.

    Using Class Name:

    1. var elem = angular.element(document.querySelector(".ClassName"));

    Using Id:

    1. var elem = angular.element(document.querySelector("#Id"));

    Using this can help in getting the HTML element in AngularJS.

    Sample Code:

    1. <html>
    2. <head>
    3. <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"></script>
    4. </head>
    5. <body>
    6. <form>
    7. <div ng-app="myApp" ng-controller="myCtrl">
    8. <input type="text" id="txtSample1" value="Hello" class="clsSample" />
    9. <input type="text" id="txtSample2" value="Hello" />
    10. <input type="button" id="btnShow" value="Show" ng-click="showval();" />
    11. </div>
    12. <script>
    13. var app = angular.module("myApp", []);
    14. app.controller("myCtrl", function ($scope) {
    15. $scope.showval = function () {
    16. alert(angular.element(document.querySelector(".clsSample")).val());
    17. alert(angular.element(document.querySelector("#txtSample2")).val());
    18. };
    19.  
    20. });
    21. </script>
    22. </form>
    23. </body>
    24. </html>

     

    Happy Coding....

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: