Reputation: 63
In angular addEventListener
is getting NULL
Below is the code in the pathname(src/assets/js/custom.js)
const signUpButton = document.getElementById('signUp');
const signInButton = document.getElementById('signIn');
const container = document.getElementById('container');
signUpButton.addEventListener('click', () => {
container.classList.add('right-panel-active');
});
signInButton.addEventListener('click', () => {
container.classList.remove('right-panel-active');
});
HTML
<!-- main section -->
<div class="container container text-center" id="container">
<div class="form-container sign-up-container">
<!-- Sign Up form code goes here -->
<form action="#">
<h1 class="text-center">Create Account</h1>
<div class="social-container">
<a href="#" class="social"><i class="fab fa-facebook-f"></i></a>
<a href="#" class="social"><i class="fab fa-google-plus-g"></i></a>
</div>
<span>or use your email for registration</span>
<input type="text" class="form-control custInp" placeholder="Name" />
<input type="email" class="form-control custInp" placeholder="Email" />
<input type="password" class="pse" placeholder="Password" />
<button>Sign Up</button>
</form>
</div>
<div class="form-container sign-in-container">
<!-- Sign In form code goes here -->
<form action="#">
<h1>Sign in</h1>
<div class="social-container">
<a href="#" class="social"><i class="fab fa-facebook-f"></i></a>
<a href="#" class="social"><i class="fab fa-google-plus-g"></i></a>
<a href="#" class="social"><i class="fab fa-linkedin-in"></i></a>
</div>
<span>or use your account</span>
<input type="email" class="form-control custInp" placeholder="Email" />
<input type="password" class="form-control custInp" placeholder="Password" />
<a href="#">Forgot your password?</a>
<button>Sign In</button>
</form>
</div>
<div class="overlay-container">
<!-- The overlay code goes here -->
<div class="overlay">
<div class="overlay-panel overlay-left">
<h1>Welcome Back!</h1>
<p>
To keep connected with us please login with your personal info
</p>
<button class="ghost" id="signIn">Sign In</button>
</div>
<div class="overlay-panel overlay-right">
<h1>Hello, Friend!</h1>
<p>Enter your personal details and start journey with us</p>
<button class="ghost" id="signUp">Sign Up</button>
</div>
</div>
</div>
</div>
<!-- main section -->
When I use this code without the use of angular works fine.
But in angular i am getting error:
Uncaught TypeError: Cannot read property 'addEventListener' of null
kindly let me know what I am doing wrong and how to resolve it?
Upvotes: 0
Views: 1146
Reputation: 382
Here You go make this changes it will work as u required add ngclass to check condition for that and and function toggleClass()
test.component.html file
<div class="container" id="container" [ngClass]="{'right-panel-active':activeClass}">
<div class="form-container sign-up-container">
<form action="#">
<h1>Create Account</h1>
<div class="social-container">
<a href="#" class="social"><i class="fab fa-facebook-f"></i></a>
<a href="#" class="social"><i class="fab fa-google-plus-g"></i></a>
<a href="#" class="social"><i class="fab fa-linkedin-in"></i></a>
</div>
<span>or use your email for registration</span>
<input type="text" placeholder="Name" />
<input type="email" placeholder="Email" />
<input type="password" placeholder="Password" />
<button>Sign Up</button>
</form>
</div>
<div class="form-container sign-in-container">
<form action="#">
<h1>Sign in</h1>
<div class="social-container">
<a href="#" class="social"><i class="fab fa-facebook-f"></i></a>
<a href="#" class="social"><i class="fab fa-google-plus-g"></i></a>
<a href="#" class="social"><i class="fab fa-linkedin-in"></i></a>
</div>
<span>or use your account</span>
<input type="email" placeholder="Email" />
<input type="password" placeholder="Password" />
<a href="#">Forgot your password?</a>
<button>Sign In</button>
</form>
</div>
<div class="overlay-container">
<div class="overlay">
<div class="overlay-panel overlay-left">
<h1>Welcome Back!</h1>
<p>
To keep connected with us please login with your personal info
</p>
<button class="ghost" id="signIn" (click)="toggleClass()" >Sign In</button>
</div>
<div class="overlay-panel overlay-right">
<h1>Hello, Friend!</h1>
<p>Enter your personal details and start journey with us</p>
<button class="ghost" id="signUp" (click)="toggleClass()" >Sign Up</button>
</div>
</div>
</div>
</div>
test.component.ts file
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-double',
templateUrl: './double.component.html',
styleUrls: ['./double.component.css']
})
export class DoubleComponent implements OnInit {
constructor() { }
activeClass=false;
ngOnInit() {
}
toggleClass(){
this.activeClass=!this.activeClass
}
}
Upvotes: 0
Reputation: 14679
Instead of manually manipulating the DOM, use what Angular has to offer. Let it handle events, handle classes, etc. All you need to do in TS is to declare a property indicating whether the panel is active
isRightPanelActive: boolean;
and in the template
<div class="container text-center" [class.right-panel-active]="isRightPanelActive">
<!-- ... -->
<button class="ghost" id="signUp" (click)="isRightPanelActive = true">Sign Up</button>
<!-- ... -->
<button class="ghost" id="signIn" (click)="isRightPanelActive = false">Sign In</button>
Upvotes: 1