# Using "this" with html button "(click)"

**URL:** <https://forum.ionicframework.com/t/using-this-with-html-button-click/76520>\
**Category:** ionic-v3\
**Created:** [January 17, 2017, 7:55pm UTC](https://forum.ionicframework.com/t/using-this-with-html-button-click/76520 "2017-01-17T19:55:18Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![georevilo](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@georevilo](https://forum.ionicframework.com/u/georevilo)\
**Post date:** [January 17, 2017, 7:55pm UTC](https://forum.ionicframework.com/t/using-this-with-html-button-click/76520/1 "2017-01-17T19:55:18Z")

</div>

i have an html file that looks like this:  
`<button id="0" (click)="Check(this.id)" > I am a button </button>`

and then i have a ts file with a page that has the Check function that looks like this:  
Check(bID)  
{  
alert("Button ID: " + bID);  
}

however the alert output is “Button ID: undefined” but i want the alert to display “Button ID: 0”

It’s super simple code not much to go wrong so I’m assuming I’m having some fundamental misunderstanding of how the way Angular works or the way Ionic works and i was hoping you guys help me out.

---

<div class="post-metadata">

**Author:** ![Sulot](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/sulot/32/100183_2.png) [@Sulot](https://forum.ionicframework.com/u/Sulot)\
**Post date:** [January 17, 2017, 8:08pm UTC](https://forum.ionicframework.com/t/using-this-with-html-button-click/76520/2 "2017-01-17T20:08:57Z")

</div>

Why “this”?

If in your component you have a variable called “id”, angular will bind it properly. “this” doesn"t mean anything in the “view”.

Yes I think you have a misunderstanding… it is a lot easier!!

---

<div class="post-metadata">

**Author:** ![ryanlogsdon](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/ryanlogsdon/32/103208_2.png) [@ryanlogsdon](https://forum.ionicframework.com/u/ryanlogsdon)\
**Post date:** [January 17, 2017, 8:18pm UTC](https://forum.ionicframework.com/t/using-this-with-html-button-click/76520/3 "2017-01-17T20:18:28Z")

</div>

“this” scopes to your class member variables, so the part you’re missing is

```
`private id: number;`

```

…in your \*.ts file.

Here’s the full solution:

**HTML**

```
`<button ion-button (click)="DoSomething(this._id)">Hello I'm {{ _id }}!</button>`

```

(you were missing the “ion-button” attribute as well)

**TS**

```
export class HomePage {

    private _id: number;

    constructor(public navCtrl: NavController) {
        this._id = 99;
    }

    DoSomething(id: number) {
        console.log('id = ' + id);
    }
}
```

---

<div class="post-metadata">

**Author:** ![ryanlogsdon](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/ryanlogsdon/32/103208_2.png) [@ryanlogsdon](https://forum.ionicframework.com/u/ryanlogsdon)\
**Post date:** [January 17, 2017, 8:19pm UTC](https://forum.ionicframework.com/t/using-this-with-html-button-click/76520/4 "2017-01-17T20:19:34Z")

</div>

btw, mine shows you the actual solution of your code’s missing private variable, but @Sulot’s answer is what you should use. 🙂

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [January 17, 2017, 8:20pm UTC](https://forum.ionicframework.com/t/using-this-with-html-button-click/76520/5 "2017-01-17T20:20:28Z")

</div>

The `id` property cannot be private. That will break when `ngc` gets involved (i.e. production builds).

---

<div class="post-metadata">

**Author:** ![ryanlogsdon](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/ryanlogsdon/32/103208_2.png) [@ryanlogsdon](https://forum.ionicframework.com/u/ryanlogsdon)\
**Post date:** [January 17, 2017, 8:21pm UTC](https://forum.ionicframework.com/t/using-this-with-html-button-click/76520/6 "2017-01-17T20:21:10Z")

</div>

it’s a generic number called ‘id’ … in my code, it’s correctly called ‘\_id’.

🙂

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [January 17, 2017, 8:21pm UTC](https://forum.ionicframework.com/t/using-this-with-html-button-click/76520/7 "2017-01-17T20:21:42Z")

</div>

I don’t care what you name it: any properties that are accessed by templates must be public.

---

<div class="post-metadata">

**Author:** ![Sulot](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/sulot/32/100183_2.png) [@Sulot](https://forum.ionicframework.com/u/Sulot)\
**Post date:** [January 17, 2017, 8:22pm UTC](https://forum.ionicframework.com/t/using-this-with-html-button-click/76520/8 "2017-01-17T20:22:54Z")

</div>

You shouldn’t call it “\_id”. As a naming convention it is often used for \_service, for example. (But you can, nothing wrong in it)

`<button ion-button (click)="DoSomething(homePageId)">Hello I'm {{ homePageId }}!</button>`

```
export class HomePage {

    public homePageId: number;

    constructor(public navCtrl: NavController) {
        this._id = 99;
    }

    DoSomething(id: number) {
        console.log('homePageId ' + id);
    }
}
```

---

<div class="post-metadata">

**Author:** ![ryanlogsdon](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/ryanlogsdon/32/103208_2.png) [@ryanlogsdon](https://forum.ionicframework.com/u/ryanlogsdon)\
**Post date:** [January 17, 2017, 8:23pm UTC](https://forum.ionicframework.com/t/using-this-with-html-button-click/76520/9 "2017-01-17T20:23:12Z")

</div>

…it works. just try it out…

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [January 17, 2017, 8:25pm UTC](https://forum.ionicframework.com/t/using-this-with-html-button-click/76520/10 "2017-01-17T20:25:24Z")

</div>

[No, it doesn’t work](https://forum.ionicframework.com/t/rc0-typescript-private-vs-public-keyword/64863/).

---

<div class="post-metadata">

**Author:** ![ryanlogsdon](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/ryanlogsdon/32/103208_2.png) [@ryanlogsdon](https://forum.ionicframework.com/u/ryanlogsdon)\
**Post date:** [January 17, 2017, 8:26pm UTC](https://forum.ionicframework.com/t/using-this-with-html-button-click/76520/11 "2017-01-17T20:26:40Z")

</div>

then update your IDE 🙂

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [January 17, 2017, 8:27pm UTC](https://forum.ionicframework.com/t/using-this-with-html-button-click/76520/12 "2017-01-17T20:27:10Z")

</div>

No way did you read that entire thread that quickly.

---

<div class="post-metadata">

**Author:** ![ryanlogsdon](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/ryanlogsdon/32/103208_2.png) [@ryanlogsdon](https://forum.ionicframework.com/u/ryanlogsdon)\
**Post date:** [January 17, 2017, 8:28pm UTC](https://forum.ionicframework.com/t/using-this-with-html-button-click/76520/13 "2017-01-17T20:28:53Z")

</div>

No offense, but please try to be productive. I’m just trying to help someone here with a coding issue. I’m not following this thread any longer. No one needs to read all this. If you want to discuss further, just IM me.

Ryan

---

<div class="post-metadata">

**Author:** ![georevilo](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@georevilo](https://forum.ionicframework.com/u/georevilo)\
**Post date:** [January 17, 2017, 8:44pm UTC](https://forum.ionicframework.com/t/using-this-with-html-button-click/76520/14 "2017-01-17T20:44:38Z")

</div>

but what if i had multiple buttons and i wanted my function to do separate things based on the id of the button pressed…  
if(bID == 2)  
//do something  
else if(bID == 0)  
//do something else

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [January 17, 2017, 8:51pm UTC](https://forum.ionicframework.com/t/using-this-with-html-button-click/76520/15 "2017-01-17T20:51:24Z")

</div>

> [@georevilo](#):
>
> what if i had multiple buttons and i wanted my function to do separate things based on the id of the button pressed

If you’re not generating these buttons via `ngFor`, it would seem much more readable to just have them have different click handlers.

If you are generating them via `ngFor`, then you can do something like this:

```auto
<template ngFor let-foo="$implicit" let-i="index" [ngForOf]="foos">
  <button (click)="onFooClick(i)">{{foo}}</button>
</template>

```

---

<div class="post-metadata">

**Author:** ![ryanlogsdon](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/ryanlogsdon/32/103208_2.png) [@ryanlogsdon](https://forum.ionicframework.com/u/ryanlogsdon)\
**Post date:** [January 17, 2017, 8:59pm UTC](https://forum.ionicframework.com/t/using-this-with-html-button-click/76520/16 "2017-01-17T20:59:20Z")

</div>

Ah ha! Now you’re seeing why my code only solved your immediate bug.

To address multiple buttons (or any other items in multiples), here’s what I like to do…

**HTML**

```
<ion-card *ngFor="let robot of _robots; let itemNum = index;">
        <ion-card-content>
            {{ robot.name }}, {{ robot.age }}
            <br />
            <button ion-button item-right (click)="DoSomething(itemNum)">Use {{ robot.name }}</button>
        </ion-card-content>
    </ion-card>

```

**TS**

```
export class HomePage {

    private _robots : any[];

    constructor(public navCtrl: NavController) {
        this._robots = [{name:'Bender', age: 4},
                        {name:'K9', age: 20},
                        {name:'Rosie', age: 50},
                        {name:'Wall-e', age: 1000}];
    }

    DoSomething(id: number) {
        console.log('id = ' + id);
        let selectedRobot = this._robots[id];
        console.log('robot name: ' + selectedRobot.name + ', robot age: ' + selectedRobot.age);
    }
}

```

You’ll see that the HTML has “let itemNum = index;” which acts as your id field.

---

<div class="post-metadata">

**Author:** ![georevilo](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@georevilo](https://forum.ionicframework.com/u/georevilo)\
**Post date:** [January 17, 2017, 9:27pm UTC](https://forum.ionicframework.com/t/using-this-with-html-button-click/76520/17 "2017-01-17T21:27:15Z")

</div>

That seems like a decent way of getting this done. thanks

---

<div class="post-metadata">

**Author:** ![georevilo](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@georevilo](https://forum.ionicframework.com/u/georevilo)\
**Post date:** [January 17, 2017, 10:44pm UTC](https://forum.ionicframework.com/t/using-this-with-html-button-click/76520/18 "2017-01-17T22:44:45Z")

</div>

Oh wait what if i wanted to do something like disable the button that was pressed? How would i reference the pressed button to set disabled?

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [January 17, 2017, 10:47pm UTC](https://forum.ionicframework.com/t/using-this-with-html-button-click/76520/19 "2017-01-17T22:47:38Z")

</div>

One way:

```auto
<button (click)="onFooClick(foo)" [disabled]="foo.disabled">

```

```auto
onFooClick(foo: Foo): void {
  if (condition) {
    foo.disabled = true;
  }
}

```

---

<div class="post-metadata">

**Author:** ![mhartington](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mhartington/32/137757_2.png) [@mhartington](https://forum.ionicframework.com/u/mhartington)\
**Post date:** [January 18, 2017, 8:19pm UTC](https://forum.ionicframework.com/t/using-this-with-html-button-click/76520/20 "2017-01-18T20:19:34Z")

</div>

Closed on January 18, 2017, 8:19pm UTC


