Uahmed
Uahmed

Reputation: 1845

get the text of div using angularjs

i want to get the text of div using angularjs . I have this code

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

where as my controller is something like this

    var  myapp= angular.module("myapp",[]);
    myapp.controller("HelloController",function($scope,$http){

    $scope.myform ={};

    function update()
    {
            // If i have a textbox i can get its value from below alert 
            alert($scope.myform.value);
    }
    });

Can anyone also recommand me any good link for angularjs . I dont find angularjs reference as a learning source .

Upvotes: 2

Views: 7799

Answers (3)

AirBorne04
AirBorne04

Reputation: 593

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/

Upvotes: 1

Mohamed Seif
Mohamed Seif

Reputation: 420

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);
 }

Upvotes: 4

Ben Taliadoros
Ben Taliadoros

Reputation: 9391

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

Upvotes: 0

Related Questions