i have a JS Class which is making a function of giving a border to an element when i click to another element. That means when i click on .trigger
class element, it will give a border to .content
class element. But this function only should be happen based on a Boolean condition. If Boolean is Yes it should be give border, other wise i should not work. My code is works when i set the method inside the constructor parenthesis with this
keyword and i can also declare variable there. But I need the method outside the constructor based on my other code. So how can i possible to declare vaiable outside the constructor and inside the class. I need this using Class approach based on my project. My code is as follows. Hope someone can help me on this. Thanks in advance!
class ParentSelector {
constructor(trigger, content, condition) {
this.trigger = document.querySelector(trigger);
this.content = document.querySelector(content);
}
let outline = condition;
makeOutline(){
this.trigger.addEventListener("click", (e) => {
if (condition) {
e.target.nextElementSibling.style.border = "2px solid red";
}
})
}
}
let a = new ParentSelector(".trigger",".content", true);
a.makeOutline();
<div class="one">
<div class="trigger">Trigger</div>
<div class="content">Content</div>
</div>