Adjust background dimensions using jQuery

Having trouble adjusting background-size using jQuery? It seems to work with a single parameter, but not with two. I'm trying to set both width and height via jQuery variables.

Here's an example of my JavaScript code:


var w = 100;
var h = 100;

$("div").css("background-size", w + "px" + h + "px");

Check out the demo here:

http://jsfiddle.net/Lv4wt75f/

This is just a basic example, in reality I need to pass two parameters to calculate the ratio.

Thank you!

Answer №1

It is essential for both of those parameters to contain a space in between them. Just adding one additional space will solve the issue.

$("div").css("background-size", w+"px " + h + "px");
                                     ^

Fiddle

Syntax Reference

Answer №2

Make sure to include a space after the px measurement.

Answer №3

$("div").attr("style", "background-size:" + width + "px " + height + "px;");
                                     ^ (Added Space)

Answer №4

$("div").css("background-size": w+"px " + h+"px");
                                      ^ (Space)

Remember to use :(colon) instead of ,(comma) after background-size

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it considered semantically correct to use ul, li, and div elements for organizing a grid layout in HTML5

Consider this scenario: You are working on a portfolio page that features a grid layout displaying 6 x 6 images. Many developers typically use ul -> li or multiple divs to achieve this design. From a semantic standpoint, is there an optimal method for ...

Is there a way to remove a specific object key from a JavaScript object?

One thing I would like to achieve is transforming a JSON structure. Let's start with an example like this: { "de": { "errors.de.i18n.js": { "errors": { "addcreditcard": "Wir konnten diese Karte nicht verifizier ...

Eliminate any blank spaces in the SELECT Angular application for IE-CSS

One issue I encountered is removing the default selected "SELECT" option from a select drop down on my webpage. Currently, I am using to remove it successfully in Chrome and Firefox browsers, but unfortunately IE does not respond to this method. I ha ...

Adding a code for divs into CSS caused the navigation bar to be completely obliterated

Initially, my menu was perfectly styled with the following CSS code in my css file: ul.Menu { list-style-type: none; margin: 10px; overflow: hidden; background-color: #ffffff; border-style: solid; border-color: #e9055c; border-width: 2px; font-weight: bol ...

In terms of performance, is it better to fetch JSON data and render it using JavaScript, or to request the full

Similar Inquiry: Why is it a bad practice to return generated HTML instead of JSON? Or is it? When making an AJAX request to a PHP file, which method would result in quicker HTML rendering? Directly sending fully formatted HTML from PHP, Or sending ...

Issue arises as Protractor's custom locator is unable to find the desired element

I decided to enhance the identification and interaction process in Protractor by adding a custom data attribute called data-test-id to specific elements. To achieve this, I created a custom locator within the onPrepare callback function in my conf.js file. ...

Encountering session timeout problem post enabling Httponly and secure attributes in Tomcat configuration file

Upon testing the application on an older version of Firefox (v42) as per requirement, I encountered an issue. I made sure to enable the Httponly and secure attributes in my web.xml file: <session-config> <session-timeout>30</session- ...

Tips for adjusting the width of v-text-field in Vuetify?

I have a v-text-field component within a toolbar that is taking up too much space. How can I adjust the width of the text-field to make it smaller? <v-toolbar dense > <v-app-bar-nav-icon></v-app-bar-nav-icon> <v-toolba ...

Remove the default CSS styling from Django CMS

When using Django CMS' text plug-in, I've noticed that the text is hardcoded with its own CSS, specifically within the p tags. Is there a way to remove this default styling? I would like for the text to inherit the styles defined in the theme I a ...

Forgetting your password with React JS

On my login page, there is a button labeled "Forget my password". When I click on this button, I want to be taken directly to the correct page for resetting my password. Currently, when I click on "forget my password", it displays beneath the login sectio ...

Missing Cookie in request using NodeJS and NextJS

Struggling with integrating cookies in a fullstack app I'm developing using Node for backend and NextJS for frontend on separate servers. The challenge lies in getting the browser to attach the cookie received in the response header from the node serv ...

Utilize Chrome storage instead of localstorage to generate Parse sessions

I'm currently developing a Chrome Extension that relies on Parse User sessions. Because localstorage is limited to specific domains, I am looking to utilize chrome.storage so the data can be accessed across any site. The existing Parse Javascript SDK ...

Guide to accessing Shopping Cart information on the Homepage

Can anyone shed light on how to retrieve the contents of a shopping cart using JavaScript while being on the home page and not the actual shopping cart page itself? Take, for instance, After adding an item to your cart on Vitamix, if you navigate back to ...

What is the best way to show only a section of a background in CSS?

Being a novice in HTML and CSS, I am facing the challenge of displaying an image as a banner with text overlay. Despite trying various methods, it seems impossible to achieve this directly with an image element, so I resorted to using CSS's background ...

Unable to maintain active status on Vuejs (PrimeVue) Sidebar component leads to malfunction

I currently have a PrimeVue Sidebar component set up with a dynamic component being passed to it. At the moment, I am only using a single component to test this functionality. <Sidebar v-model:visible="sidebarState" :baseZIndex="1000" ...

Reload iframe content using a .php file within a different iframe

I am currently working on a page that consists of 7 different iframes: <iframe id="leftframe" src="structure/leftbar.php"></iframe> <iframe id="headerframe" src="structure/header.php"></iframe> <iframe id="menuframe" src="struct ...

Error: The function this.saveAlpha is not recognized in the current context at eval

This code features a start button, stop button, and a timer. The goal is to retrieve information from the phone using events like DeviceOrientationEvent, which includes properties such as absolute, alpha, beta, and gamma (referenced in this link: here). I& ...

Flexbox keeps elements on the same line without breaking

I'm currently delving into flexbox and aiming to create a layout similar to the one shown below: https://i.sstatic.net/epnkU.png Here is the code I have come up with: .container { display: flex; gap: 26px; } .flex50 { flex: 50%; ...

Unexpected results occurring during JavaScript refactoring process

Having this repetitive code snippet that switches between two radio buttons being checked within my $(document).ready(): $(document).ready(function () { $("#New").click(function () { var toggleOn = $("#New"); var tog ...

What could be causing this React/Javascript object to be undefined?

I've been attempting to convert this JSON into JavaScript objects, but all my efforts have resulted in undefined values. The objects simply won't work as intended. Here is the code I'm using with the getStaticProps method to extract objects ...