Skip to content Skip to sidebar Skip to footer

Get The Text Of Div Using Angularjs

i want to get the text of div using angularjs . I have this code
Here
where as my controller is something like this

Solution 1:

You should send the click event in the function, your html code should be :

<div ng-click="update($event)"id="myform.value">Here </div>

And your update function should have the event parameter which you'll get the div element from and then get the text from the element like this :

function update(event)
 {
  alert(event.target.innerHTML);
 }

Solution 2:

i just thought i put together a proper answer for everybody looking into this question later.

Whenever you do have the desire to change dom elements in angular you need to make a step back and think once more what exactly you want to achieve. Chances are you are doing something wring (unless you are in a link function there you should handle exactly that).

So where is the value comming, it should not come from the dom itself, it should be within your controller and brought into the dom with a ng-bind or {{ }} expression like:

<div>{{ likeText }}</div>

In the controller now you can change the text as needed by doing:

$scope.likeText = 'Like';
$scope.update = function() {
     $scope.likeText = 'dislike'; 
}

For angular tutorials there is a good resource here -> http://angular.codeschool.com/

Solution 3:

Redefine your function as

$scope.update = function() {
 alert($scope.myform.value);
}

A better way to do it would be to use ng-model https://docs.angularjs.org/api/ng/directive/ngModel

Check the example, these docs can be a bit wordy

Post a Comment for "Get The Text Of Div Using Angularjs"