function changeCss(node, selector, property){ node.style.ВОТ СЮДА = property; } 
  • Your question is not very clear. Tell me more about what to do. - Yuri
  • In the changeCss function after .style, insert an argument such as backgroundImage or margin - Denisoed
  • @DenisoDeniso Do you have a question about selectors? - Vadim Ovchinnikov
  • What is the role of the node argument? - Vadim Ovchinnikov
  • Argument node - in my case h3 - Denisoed

2 answers 2

We write the changeCss function, which accepts a selector selector , a CSS property and the value for this property value :

 function changeCss(selector, property, value) { var nodes = document.querySelectorAll(selector); for (i = 0; i < nodes.length; ++i) { nodes[i].style[property] = value; } } 

Call examples

 function changeCss(selector, property, value) { var nodes = document.querySelectorAll(selector); for (i = 0; i < nodes.length; ++i) { nodes[i].style[property] = value; } } changeCss("#img", "width", "200px"); changeCss("#img", "height", "200px"); changeCss("#img", "background-image", "url(http://lorempixel.com/output/nature-qc-200-200-6.jpg)"); changeCss(".text", "margin", "10px"); changeCss(".text", "color", "red"); changeCss(".text", "font-size", "50px"); 
 <div id="img"> </div> <div class="text"> This is text </div> 

    Use the eval function, which turns a string into code:

     function changeCss(node, selector, property) { eval(node+'.style.'+selector+' = "'+property+'"'); }; 

     function changeCss(node, selector, property) { eval(node+'.style.'+selector+' = "'+property+'"'); }; $(function() { changeCss('document.getElementById("text")', 'color', 'red'); }); 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="text">Текст</div> 

    • Oh and naughty. With eval you need to be careful, because you can get anything here. - Vadim Ovchinnikov
    • @VadimOvchinnikov, yes, I am like this: D This is the first thing that came to mind - Yuri