I need js code:: when I click outside the modal(login or signup) it has to close for both login and signup modal

I have written a javascript code to close login modal and signup modal if I click somewhere outside those modals.But that property working for either login modal or for signup modal. My requirement is when I open a modal(login or signup) and clicks somewhere outside the modal it has to close.? I’m not good at javascript please help me….

var signup_form = document.getElementById('id02');
window.onclick = function(event) {
    if (event.target == signup_form) {
        signup_form.style.display = "none";
    }
}
//signup js ends
// login form js starts
var login_form = document.getElementById('id01');
window.onclick = function(event) {
    if (event.target == login_form) {
        login_form.style.display = "none";
    }
}
//login form js ends```
if this property working for login modal then not working for signup modal. viceversa

Answer

Rather than two events, just add an else if to one of the events:

var signup_form = document.getElementById('id02');
var login_form = document.getElementById('id01');
window.onclick = function(event) {
    if (event.target == signup_form) {
        signup_form.style.display = "none";
    } else if (event.target == login_form) {
        login_form.style.display = "none";
    }
}

As Pointy said, there cannot be two events for one element