javascript - Add css properties to a collection from an object -


i need add css properties on each element of collection jquery. suggestions?

var jsoncss = {     "color": "#555555",     "font-weight": "bold",     "text-align": "left" };  var $tdcollection = $("tr:first-child").find("td:nth-child(2)"); //a collection of <td> elements $.each($tdcollection, function(k, v){     v.css(jsoncss); }) 

you don't need use each() here, can apply css settings elements within jquery object:

var cssobject = {     "color": "#555555",     "font-weight": "bold",     "text-align": "left" };  var $tdcollection = $("tr:first-child").find("td:nth-child(2)"); $tdcollecton.css(cssobject); 

also note object define holds css rules nothing json @ - it's object - hence why changed name.


Comments