How do I set a css property with a variable in JQuery?

I need to set the CSS width property to be the same size as the width of the current browser. For this I did var setWidth = $(window).width()

, but I don't know how to apply this to my current code, something like this:

$(document).ready(function(){
    $("#backgroundImg").css("width", ""); 
});

      

Is there a way to do this? I am new to JQuery so I might be really hobbyist.

+3


source to share


4 answers


try it

$(document).ready(function() {
    var setWidth = $(window).width();
    $("#backgroundImg").css("width", setWidth + 'px'); 
});

      



or you can use $.width

$(document).ready(function() {
    $('#backgroundImg').width( $(window).width() )
});

      

+7


source


update code to next



$("#backgroundImg").css("width", setWidth + "px");

      

0


source


One option is $ ("# backgroundImg"). css ("width", "100%");

with a variable parameter: var setWidth = $ (window) .width () + 'px';

so your code will be

$(document).ready(function(){
$("#backgroundImg").css("width", setWidth); 

      

});

0


source


This should make the background match the current browser width:

$( "#backgroundImg" ).width( setWidth )

      

Source

0


source







All Articles