One thing I shall be specializing in regularly right here at JSP is producing tidy code that is simpler to take care of sooner or later in addition to nicer to work with. One such space the place folks usually produce messy code is when utilizing jQuery’s .css() technique.
Fundamental utilization of this goes like so:
$(elem).css(“show”); //returns worth
$(elem).css(“show”, “block”); //units worth
And in an effort to set a number of values, folks will usually do that:
$(elem)
.css(“show”, “block”)
.css(“border”, “1px stable blue”)
.css(“background”, “#F00”)
{and so forth}
Nonetheless that is the unsuitable approach to do that, on so many ranges. jQuery is written fairly cleverly, and .css() can take an object of properties & their respective values:
$(elem).css({
“border” : “1px stable blue”,
“show” : “block”,
“background” : “#F00”
});
It is value noting that you do not really need the quotes across the properties (however you do across the values), this is able to be legitimate:
$(elem).css({
border: “1px stable blue”,
{so on}
});
Nonetheless, you need to use quotes if you wish to use a reserved phrase as a property. For instance for those who have been creating a brand new ingredient & setting the category attribute:
$(“<div/>”, {
class: “myDiv”
});
That is invalid, as class is a reserved phrase, so that you’d must do :
$(“<div/>”, {
“class” : “myDiv”
});
As a result of this at all times journeys me up, I’ve merely gotten into the behavior of at all times placing quotes round my property names in JS objects. That is extra a private choice than the rest, simply decide what makes essentially the most sense to you.
Subscribe to MarketingSolution.
Receive web development discounts & web design tutorials.
Now! Lets GROW Together!