1

I have some function like below, that sets inline css attribute to the elements. It's kinda very simplified JQuery's .css() method:

function cssSetAttr(styleName, value) {
   for (var i = 0, n = someObject.length; i < n; i++) {
        someObject[i].setAttribute('style', [styleName][value]);
}
someObject.cssSetAttr('color', 'red');

Problem is how to put arguments into setAttribute like this:

setAttribute('style', 'styleName: value') 
2
  • use display.style Commented Jul 10, 2018 at 10:47
  • I'd suggest creating a different function for styling properties. Then you can call it like this: setCSS('propertyName', 'value') and use this internally within the function: element.style[propertyName]. This has the benefit that you can call it multiple times on the same element without overwriting any previous values. Commented Jul 10, 2018 at 10:48

1 Answer 1

2

You can use template literals

setAttribute('style', `${styleName}: ${value}`);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.