innerHTML returning undefined when <p></p>
tag is clicked, but i am expecting its inside html. Any guess why?

- 795
- 2
- 13
- 37
-
Please provide the relevant code here in the question, not in an external site. Edit the question accordingly. – Luiggi Mendoza Feb 06 '15 at 17:29
4 Answers
Because Angular passes $scope
objects, not the DOM elements. Since $scope
object doesn't have the innerHtml
, you are getting undefined
.
You can assign a model to either an input or a textarea, then pass this model to your function instead. Here is a plunker: http://plnkr.co/edit/VjFXacXJMa0PpS364wOb Or, if you want to stick with playing with DOM from controller which is against the nature of Angular, you can use $event.target
.

- 6,417
- 5
- 39
- 53
You can pass angular's $event
<p ng-click="abc($event)">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptas, nemo, numquam, incidunt similique quisquam dolor blanditiis quis veniam at laudantium quibusdam beatae ad ea voluptates impedit molestiae cum modi quam!
</p>
and get the innerHtml
$scope.abc = function(e) {
alert(e.target.innerHTML);
}

- 4,477
- 3
- 34
- 59
You need $event.target
or $event.currentTarget
.
I would you should use $event.currentTarget
, beacuse it will give you more precise element where you clicked.
CODE
$scope.abc = function($event){
//more precise element tracking will track sub element too using $event.currentTarget
alert( $event.currentTarget.innerHTML || $event.srcElement.innerHTML);
alert($event.target.innerHTML); //element tracking
}
Refer this answer for more info.

- 1
- 1

- 134,766
- 23
- 234
- 299
instead of this you can pass $event
and get $event.target
which is the clicked dom element
$scope.abc = function($event){
alert($event.target.innerHTML);
}

- 5,364
- 4
- 43
- 59