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

193
Views
Is there any way that I can use variables while changing the the properties of document.getElemtentbyId(").style in JavaScript?

Is there any way that I can change the properties in document.getElementbyId.style with a variable that I received from the Parameters of the function?

function toggleFunction(elementId, property1, property2) {
    let element = document.getElementById(elementId)
    if (element.style.display == property1) {
        element.style.display = property2
    } else {
        element.style.display = property1
    }
}

The above code is not giving me the desired result. Is there a way that I can use variable values that I got from parameters for writing Properties?

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

0

Maybe I didn't catch what you need but its simple. Your code should work good if you use correct arguments for you func. But don't use "element" name twice. That is bad practise

const elementId = 'your-element-id'
const property1 = 'none'
const property2 = 'block'

const element = document.getElementById(elementId)
element.style.display = property1 // Set element's display = 'none'

The same way for another style properties:

element.style.width = '50%'
element.style.color = '#ff0000'
element.style.marginTop = '14px'

Read more -> HTML DOM Style Object

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!