javascript - How to send current div's id into a function when clicked - Stack Overflow

I'm trying to send a div's id into a function as a parameter when clicked. But it is not work

I'm trying to send a div's id into a function as a parameter when clicked.

But it is not working.

Here's what I have written, please take a look :

<div id="currentId" ng-click="functionCalled(id)">
</div>

I'm trying to send a div's id into a function as a parameter when clicked.

But it is not working.

Here's what I have written, please take a look :

<div id="currentId" ng-click="functionCalled(id)">
</div>
Share Improve this question asked May 18, 2017 at 9:55 HarishHarish 1,2738 gold badges24 silver badges47 bronze badges 2
  • Is it a string you're passing in as an id? If so have you tried ng-click="functionCalled('currentId')" ? – rrd Commented May 18, 2017 at 9:57
  • see this – anoop Commented May 18, 2017 at 10:05
Add a ment  | 

5 Answers 5

Reset to default 4

Edit your code with,
HTML:

<button id="bla" class="button button-light" data-ng-click="functionCalled($event)">
    Click Me
</button>

JS:

$scope.functionCalled= function(event){
    alert(event.target.id);
}

try to use following method with this object <div id="currentId" ng-click="functionCalled(this.id)"> </div>

The click event function has the event as an argument.

<div id="currentId" ng-click="functionCalled(event)">

Now you can access the properties from the target property in the in the event argument.

function functionCalled(event){
   let id = event.target.id; 
}
<div id="currentId" ng-click="functionCalled($event)">
</div>

In your controller

functionCalled($event){
//use $event.target.id
}

you can pass this as $event

 <div id="currentId" ng-click="functionCalled($event)"></div>

and use this as

 e.target.attributes.id.value

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745066074a4609262.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信