document.body.style.backgroundColor="#F00";
This statement styles the body background color as red.
In my original code I have a function that assigns elements by tag name to a global variable element
. I've found after calling this function I can do this: element.style.backgroundColor="#F00";
However, there doesn't seem to be ANY way to substitute for the property section of the statement. Below I've produced the most obvious approach, though I have also tried assigning the parameters to variables and various combinations of quotation.
function assignStyle(value) {
document.body.style.backgroundColor = value;
}
function attemptStyle(property,value) {
document.body.style.property = value;
}
button { font-family: monospace; }
<button onmouseover="assignStyle('#131519');" onmouseout="assignStyle('#FFF');">
function assignStyle(value){}
</button><br />
<button onmouseover="attemptStyle('backgroundColor','#137619');" onmouseout="attemptStyle('backgroundColor','#FFF');">
function attemptStyle(property,value){}
</button>
Edit: This question is not a duplicate of How do I add a property to a JavaScript object using a variable as the name?. That question contains jQuery, which many people, including myself, know literally nothing about. My question is concerned with basic JavaScript. Also, I want to know how to add a property to an element, and so by means of passing an argument to a function parameter. As far as I'm concerned, variables complicate the question further.