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

204
Views
How to check if the given string is a valid 'PropertyName' and 'PropertyValue' for the style attribute of an HTML element?

I would like to dynamically add/edit a property of the style attribute based on the user input. The user can give the Property Name and Property Value as a string. I would need to validate, if the given string is a valid property name/property value for the style attribute of an HTML element before applying.

Is there any way to achieve the above validation?

Is there any javascript function that can validate the given string for valid property name/property value or function that returns all valid properties/values for a particular property of the style attribute

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

0

The idea is applying the style and checking for its existence. This function can use some improvements but it works or at least demonstrates the concept.

function is_valid_css_prop(prop, value) {
  var div = document.createElement("div");
  document.body.appendChild(div)
  div.style[prop] = value;
  var obj = getComputedStyle(div);

  var found = false;
  for (var key in obj) {
    if (key == prop && value == obj[key]) {
      found = true;
      break;
    }
  }
  document.body.removeChild(div)
  return found;
}


console.log(is_valid_css_prop("position", "above"));
console.log(is_valid_css_prop("position", "absolute"));

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!