kaja111
kaja111

Reputation: 59

function is not defined at HTMLButtonElement.onclick Angular 4

I am new to Angular and I have this error:

"openClose is not defined at HTMLButtonElement.onclick (index:13)"

I searched up every possible answer to this, but the thing is that the error is in the index page and not in any of the app files. I will paste my code here in hope that someone knows what to do.

app.component.html

<button id="btn1" class="btn1" onclick="openClose(this, 'bg-modal')">Click me </button>
    <div class="bg-modal" id="bg-modal">
        <div class="modal-content">
      <div class="col-12">
        <div class="close" id="close">+</div>
        <h2>Modal</h2>
        <hr>
      </div>
      <div class="col-12">
        <div class="contents">
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
        </div>
        </div>
      <div class="confirm" id="confirm">OK</div>
      </div>
    </div>

app.component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css',
  './video-style.css']
})
export class AppComponent {}

function openClose(btn1, bgmodal){
    document.getElementById('.btn1').addEventListener('click', function(){
    document.getElementById('.bg-modal').style.display='flex';
    });
}

index.html

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Modal</title>
  <base href="/">
  <meta name="viewport" content="width=device-width, initial-scale=1">

</head>
<body>
<app-root></app-root>

</body>
</html>

I have tried:

Can anyone help?

Upvotes: 3

Views: 8662

Answers (3)

Hoque MD Zahidul
Hoque MD Zahidul

Reputation: 11949

If you are using angular you should mention which event you want to use. If you want to use click event then you should use like this :

<button (click)="onSave()">Save</button>

because angular doesn't have any event like

onclick

Upvotes: 0

programoholic
programoholic

Reputation: 5194

This is how it should be. :

<button id="btn1" class="btn1" (click)="openClose($event, 'bg-modal')">Click me </button>

In Component.ts file. :

public openClose(event, bgmodal){
       .... rest of code 
    }

Note that : Direct accessing of DOM element is not recommended in Angular.

Just to make your scenario working adding a Stackblitz demo : Demo

Upvotes: 8

Tran Dong
Tran Dong

Reputation: 116

onclick at button can not call function of component, it must call with javascript code, eg: onclick="console.log('sss')"

If u want to call to function of component:

.html:

<button id="btn1" class="btn1" (click)="openClose(this, 'bg-modal')">Click me </button>

.ts:

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css',
  './video-style.css']
})
export class AppComponent {
    openClose(btn1, bgmodal){
        document.getElementById('.btn1').addEventListener('click', function(){
            document.getElementById('.bg-modal').style.display='flex';
        });
    }
}

Upvotes: 1

Related Questions