0

Hii i need to trigger a click in first elements #first > #second > #third > .element-for-trigger-click , see here

<div id="first" ng-repeat="competition in prematchGameViewData track by competition.id">
    <div id="header">...</div>
    <div id="details">...</div>


    <div id="second" ng-repeat="(groupDate, groupGames) in competition.gamesGroupedByDate">     
        <div id="third" ng-repeat="prematchGame in groupGames track by prematchGame.id">
            <div class="element-for-trigger-click"></div>
            ...
        </div>
    </div>
</div>

Question : How can i get event from Left controller ,find .element-for-trigger-click and trigger a click on right controller with native js . enter image description here

Mistalis
  • 17,793
  • 13
  • 73
  • 97

2 Answers2

0

Aro,

so in following thread you can find information how to 'simulate' click event via native JS on given element (tough by id)

How to simulate a click with JavaScript?

however I really would not recommend that kind of approach, especially if you use angularJS, as there are ways to do controller to controller communication with angular lib itself like:

  • using of angular event bus ($emit, $broadcast and $on)
  • using a services
  • using pattern of state container like redux (https://github.com/reactjs/redux)
  • and probably few more.
Community
  • 1
  • 1
DonCziken
  • 693
  • 1
  • 8
  • 13
  • Hi @DonCziken , thanks for answer , Yes im absolutely agree with you its right way to learn programming ,but its not my project i just do here some edits like this , and i must do it asap ( im not expert angularjs developer) if you now better ways please help me –  Mar 19 '17 at 22:18
  • @Aro well If you just need to do it quick & dirty I think you can just apply the answer from question I've linked you to. – DonCziken Mar 19 '17 at 22:36
0

You can dispatch a click event with:

var evt = document.createEvent("MouseEvents");
evt.initMouseEvent("click", true, true, window, 1, 0, 0, 0, 0,
    false, false, false, false, 0, null);

var target = document.getElementById('third').firstChild;
target.dispatchEvent(evt);

Btw, you wouldn't have to use .firstChild if you simply gave that element an ID like all your other elements.

Joey
  • 10,504
  • 16
  • 39
  • 54