admin管理员组

文章数量:1389412

I am trying to get boolean value, if element id has been clicked in angular. I have tried to get boolean value through document.getElementById to find element clicked or not, but that is not working as expected.

This is how my sample html code:

<button id="my-btn-id" class="bx--number__control-btn up-icon">

If my button id will be clicked, I need to get true or need to return false. Here through click event I can able to get it, but I need to be identified through id. If any one have any idea please help me.

I am trying to get boolean value, if element id has been clicked in angular. I have tried to get boolean value through document.getElementById to find element clicked or not, but that is not working as expected.

This is how my sample html code:

<button id="my-btn-id" class="bx--number__control-btn up-icon">

If my button id will be clicked, I need to get true or need to return false. Here through click event I can able to get it, but I need to be identified through id. If any one have any idea please help me.

Share Improve this question edited Apr 22, 2019 at 14:31 Roy asked Apr 22, 2019 at 14:24 RoyRoy 9108 gold badges25 silver badges44 bronze badges 3
  • 1 If it is angular then go angular way...But in most cases you don't need id for such scenarios. If you could explain bit more about your issue then suggesting any answer would bee easier. – Jai Commented Apr 22, 2019 at 14:28
  • isn't stopping the form submission would get it solved? Can you add your form sumission code also in your post? – Jai Commented Apr 22, 2019 at 14:44
  • @Jai Yeah I got it...Through event listener I can get element id and problem resolved..Thanks – Roy Commented Apr 22, 2019 at 15:13
Add a ment  | 

2 Answers 2

Reset to default 5

Just a add a variable in your class, say: public myBtnIdClicked: boolean = false

<button id="my-btn-id" (click)="myBtnIdClicked = true" class="bx--number__control-btn up-icon">

Add a click listener on your button, set the variable myBtnIdClicked to true once a click happens over the button.

This is probably not exactly what you want but why not keep it simple. I like simple.

controller

public clickedBtn1: boolean = false; // keep a number which is the same as button id.

Then you can always check by simply.

if (this.clickedBtn1)
{
     //do something 
     // will only be reached if button has been clicked
} 

template

<button id="1" class="bx--number__control-btn up-icon" (click)="clickedBtn1 = true">

本文标签: javascriptHow to get boolean valueIf element id clickedAngularStack Overflow