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

108
Views
Add Class Function as a Module

I am wanting to consolidate a function into a module so it can be reused on several elements as opposed to having to write a new function out for each DOM element I want to use it on.

The function adds a class to a div via a listener input event. So...

When "Music" input is populated then "div class="music-input" shows. When "Age" input is populated then "div class="age-input" shows. When "Hair" input is populated then "div class="hair-input" shows. etc etc

At the moment I have the following function:

<input type="text" id="music-taste" name="properties[Music Taste]"/>  
<input type="text" id="your-age" name="properties[Your Age]"/>  
<input type="text" id="hair-colour" name="properties[Hair Colour]"/>  

var music_req = document.querySelector("#music-taste");
var age_req = document.querySelector("#your-age");
var hair_req = document.querySelector("#hair-colour");

function woolShow(){
    const showMusic = document.getElementById("music-input");
    const showMusic_classes = showMusic.classList;
    showMusic_classes.add("show-div");
}
music_req.addEventListener("input", woolShow);

This adds the "show-div" class to the existing div to show the content (I do this via CSS)

div#music {
    opacity: 0;
    transition:opacity 0.5s linear;
    padding: 20px;
    background: #e5e5e5;

}
div#music.show-div {
    opacity: 1;
}

I was just wondering if I can reuse the woolShow() function somehow using parameters to save me having to write out a different function for each div / input - Music, Age, Hair.

about 4 years ago · Juan Pablo Isaza
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!