0

I'm trying to dynamically change an elements CSS using a JSON like so...

cssSettings = {
  fontSize: function(fontSize) {
    $(".content").css("font-size", fontSize + "px")
  },
  color: function(color) {
    $(".content").css("color", color)
  }
}

var settings = {
  fontSize: "24",
  color: "red"
}

cssSettings(settings)
body {
  background: #9effe5;
  padding: 1em;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="content">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolor dolorum architecto laudantium porro odit! Eligendi consequatur, totam molestiae. Nam laborum quam, accusamus quis distinctio corporis voluptatum repellendus optio voluptatibus dolorum!</div>

Might be sleep deprivation, but I can't seem to figure out how to do it this way.

Can someone help explain exactly what I doing wrong?

1
  • 1
    why are you using cssSettings as a function while it's a plain object ? Commented Mar 28, 2016 at 0:21

3 Answers 3

2

One issue is the fact that you have cssSettings = {...} in your code, but later attempt to invoke it as a function: cssSettings(settings).

My honest opinion is that it seems you're adding an additional layer on top of jQuery's css function that isn't necessary, because if you take a look at their .css() api, by default it's able to handle a plain object directly.

So I would actually refactor it to:

var settings = {
  fontSize: "24",
  color: "red"
}

$(".content").css(settings);

This way you don't introduce additional overhead that isn't really needed.

Sign up to request clarification or add additional context in comments.

Comments

1

You could do something like this. (Note untested.)

Css settings needs to be a function not an object. The way i tackled it was to loop over the settings object and if there is a matching function in the mySettings object it will call the function with that objects params.

var settings = {
  fontSize: "24",
  color: "red"
}

var cssSettings = function(opts){
  var selector = opts.selector || ".content":
  var mySettings = {
    fontSize: function(fontSize) {
      $(selector).css("font-size", fontSize + "px")
    },
    color: function(color) {
      $(selector).css("color", color)
    }
  };

  for (var i in opts) {
    if (mySettings[i] && typeof mySettings[i] === 'function'){
      mySettings[i](opts[i]);
    }
  }

};


cssSettings(settings)

I actually like this idea seems clean

1 Comment

I didn't even think about using a for loop. Thanks
0

Use CSS classes like so:

.error {
    color: red;
}

.tiny {
    font-size: 0.65em;
}

.small {
    font-size: 0.8em;
}

... (etc) ...

Apply them with jQuery like so:

$(selector).addClass("error");
$(selector).addClass("tiny");

This separates the logic that updates the design from the design itself.

Placing the colors and font sizes in the JavaScript violates the Separation of Concerns https://en.wikipedia.org/wiki/Separation_of_concerns#HTML.2C_CSS.2C_JavaScript

1 Comment

I already know this. This wasn't the focus of the problem. Although it is good practice.

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.