One approach that comes to my mind is to pre-populate the arrays with actual css values, and then do a search -
var validValues = { 'textAlign': ['left', 'right', 'center', 'justify', 'start'] }; var userSpecifiedCSSValue = documet.getElementById('foo').style.textAlign.toLower(); if (validValues.textAlign.indexOf(userSpecifiedCSSValue) > -1) {
Note: indexOf not available for IE <= 8.
If you want to make it shared -
var userStyle = document.getElementById('foo').style, userSpecifiedValue, allowedCSSValues; for (cssProperty in userStyle) { if (userStyle.hasOwnProperty(cssProperty) && validValues.hasOwnProperty(cssProperty)) { userSpecifiedValue = userStyle[cssProperty].toLower(); allowedCSSValues = validValues[cssProperty] if (allowedCSSValues.indexOf(userSpecifiedValue) > -1) {
If you also want to enable browser-specific validation, you can create a list of valid values based on your browser type. Just create another layer in the object -
var validValues = { 'chrome': { 'textAlign': []
However, for me it still looks like a hacker solution.
source share