4

I am generating a list as below

<div ng-repeat="list in fileUploadList">
    <div id="{{list}}Upload"></div>
</div>

Inside the controller I have to get the element by ID so I am using this line:

document.getElementById($scope.fileUploadList[0] + 'Upload')

I logged the result to the console but it is returning null. What should I do?

Function in controller

$scope.loadHandleImage = function () {

     console.log(document.getElementsById($scope.fileUploadList[0] + 'Upload'));
            for (var i = 0; i < $scope.fileUploadList.length; i++)
            {  
                document.getElementById($scope.fileUploadList[i] + 'Upload').addEventListener('change', handleImage, false);   
            }

where handleImage is a function to be called

Edric
  • 24,639
  • 13
  • 81
  • 91
saikat mukherjee
  • 355
  • 1
  • 3
  • 17

6 Answers6

1

I have got the problem document was not ready when you were trying to access the element use angular.element(document).ready method

angular.element(document).ready(function() {
console.log(document.getElementById($scope.fileUploadList[0] + "Upload"))
});

here is working example http://jsfiddle.net/hqqewnyc/

jitender
  • 10,238
  • 1
  • 18
  • 44
0

Use

angular.element('#' + $scope.fileUploadList[0] + 'Upload').on('change', handleImage)

instead of document.getElementById and addEventListener

MeTe-30
  • 2,512
  • 2
  • 21
  • 30
0

A simple way would be (if you dont care about your list object not being in the random id)

<div ng-repeat="list in fileUploadList" >
  <div id="{{$index}}Upload" ></div>
</div>

Then in the controller get the id by

document.getElementById('0Upload')
document.getElementById('1Upload')

..etc

Srijith
  • 1,434
  • 9
  • 14
0

Not angular expert but based on my DOM experience there are ways to get ID attribute. The way i do in normal javascript is here

str = "";
for(tmp=0;tmp<10;tmp++)
{
    str = str + "<div id='myid" + tmp + "' onclick='idofobject(\"myid" + id + "\")'>Click Me</div>";
}

i created html with DIV and dynamically assign it a id like myid1 myid2 etc. there is a function call for each div idofobject that takes the id

just a hint that there can be ways to get dynamic id of objects

Shahzad Aslam
  • 172
  • 1
  • 12
0

As each div gets dynamic ID, so the dynamic elements should be fetched using parent child relation. EX: If you add 2 button in a div dynamically fetch the first and second child of that div to get each button.

  • You can see in my given code I am calling them dynamically.. :) – saikat mukherjee Jul 07 '16 at 06:51
  • Please see the below thread you need to get the child elements using the outer div , you should give the outside div a id and they get child for that id: http://stackoverflow.com/questions/19500592/javascript-get-child-nodes-of-a-div – Sumit Chawla Jul 07 '16 at 09:33
0

instead of ids use a classname on the elements then get elements by class name - this will give you some kind of array you can then loop through and get whatever attribute you want off each element.

matpol
  • 3,042
  • 1
  • 15
  • 18