Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

93
Views
How can i use variable inside Vanilla JavaScript class to make a function based on boolean

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, otherwise it should not work. My code works when I set the method inside the constructor parentheses 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 declare variable outside the constructor and inside the class. I need this using Class approach based on my project.

My code is as follows.

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>

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

First, why some onClick whose only use case is the if-condition? Shouldn't you be like, inside makeOutline() :

makeOutline() {
    if (this.condition) {
      this.trigger.addEventListener("click", (e) => {
        e.target.nextElementSibling.style.border = "2px solid red";
     })
    }
  }

?

Second, why are you trying to set local variables outside constructor/methods?

UPDATE: I see you're asking about declaring a class field outside of its constructor. In that case, you declare it at the top of the class. The following should work:

class ParentSelector {
  condition = false;

  constructor(trigger, content, condition) {
    this.trigger = document.querySelector(trigger);
    this.content = document.querySelector(content);
    this.condition = condition;
  }

  // ...
}

Feel free to ask me any questions you have about this!

about 4 years ago · Juan Pablo Isaza Report

0

at last i able to find the solution myself after a deep thinking. Following is the solution. Following is the code.

class ParentSelector {
    constructor(trigger, content, condition) {
        this.trigger = document.querySelector(trigger);
        this.content = document.querySelector(content);
        this.outline = condition;
    }
    
        makeOutline(){
        this.trigger.addEventListener("click", (e) => {
            if (this.outline) {
                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>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!