Search code examples
javascriptjquerycssbackground-imagebackground-size

Is it possible to enter a background image size with variable?


var setSize = $('#inputSize').val();

$("#canvas").css({
  'background-size': '100px' + setSize
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="inputSize" class="form-control" type="number" value="">


Solution

  • Yes you can, but there is something off in your code.

    better do it this way

    var setSize = parseFloat($('#inputSize').val());
    var size = isNaN(setSize) ? 0 : setSize;
    $("#canvas").css({
      'background-size': 100 + size + 'px'
    });