Modifying CSS Styles using JQuery

I've been attempting to use JQuery to change a CSS property without success, despite trying multiple solutions found on SO.

Here's what I've attempted (with numberHours being a previously defined variable):

$(".cd-schedule .events .top-info").css("height", 1000 / numberHours + "px");

Even breaking it down into three separate instructions for each css class didn't yield results.

This snippet shows my current CSS code:

@media only screen and (min-width: 800px) {
  .cd-schedule .events .top-info {
    display: table;
    height: 50px; /*<-- The property I want to change*/
    border-bottom: 1px solid #EAEAEA;
    padding: 0;
  }
}

Answer №1

Instead of relying on jQuery, consider using a CSS Variable (custom property) to dynamically update the value like this:

Check out this Codepen demo for reference


CSS

:root {
   --infoheight: 50px;
}

.top-info {
    display: table;
    border-bottom: 1px solid #9bc;
    padding: 0;    
    height: var(--infoheight); 
    transition: height 1s 0s;
}

JS

let root = document.documentElement;
let numberHours = 5;

document.getElementById('btt').addEventListener('click', function() {
   root.style.setProperty('--infoheight', 1000 / numberHours + 'px')   
});

Answer №2

$('#clickMe').on('click', function(){
    $('#content').css('font-size','20px');
});
#content{
font-size:16px;
color:blue;
}
<script src="https://cdn.jquery.com/jquery-3.5.1.min.js"></script>
<div id='content'>Change my font size</div>
<br>
<button id='clickMe'>Click to change font size</button>

Answer №3

An additional demonstration showcasing the use of floating-point-pixel/integer-pixel values for setting height and comparing computed CSS properties with style attributes. While I typically prefer using integer pixel values, floats also appear to function well. It is important to ensure that numberHours is never zero. Check out the demo here

Answer №4

Before setting the height, consider creating a variable called newHeight. This way, in your query, you can utilize a variable instead of plain JS text.

var numberHours = 100;
var newHeight = 1000 / numberHours + "px";
$(".cd-schedule .events .top-info").click(function () {
  $(".cd-schedule .events .top-info").css("height", newHeight);
});
.top-info {
  background: black;
  height: 100px;
  width: 100px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="cd-schedule">
  <div class="events">
    <div class="top-info"></div>
  </div>
</div>

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

Bug in Responsive Mega Menu Detected

Hey everyone, I've got a Mega Menu below for you to check out! <div id="menu-wrapper"> <ul class="nav"> <li><a href='#'>Brands</a> <div> <div class="nav-colu ...

Attach functions to elements added dynamically

I've been struggling with trying to attach functions to newly added elements using jQuery. Despite experimenting with various online examples, I haven't been able to get anything to work properly. My setup involves a form with an ID and a button. ...

Organizing an array of objects within MUI cards based on social media platforms

I'm currently developing an application that showcases athlete data in a grid layout using MUI Grid. The colored borders on the left side of each card are determined by the corresponding social network associated with that athlete. https://i.sstatic. ...

Interested in retrieving the dynamically changing value of LocalStorage

Hopefully I can articulate my issue clearly. I am implementing a feature where CSS themes change upon button clicks. When a specific theme button is clicked, the corresponding classname is saved to LocalStorage. However, since the key and value in LocalSt ...

When the div element reaches the top of the page, it sticks to the top and is only displayed when the user

In the center of a full-screen hero section, there is a form. When it reaches the top, its position becomes fixed and additional classes are added through a script - such as shrinking its height and adding a background. What I aim to achieve is for the fo ...

Designed radio buttons failing to activate when focused using a keyboard

I'm facing an issue with radio input buttons that I've dynamically added to a div using jQuery. They function properly when clicked with a mouse, but do not get activated when using the keyboard tab-focus state. NOTE: To style the radio buttons ...

What is the method to retrieve content from a different domain using .load()?

When I try to retrieve data from a different domain using .load() or other jQuery ajax functions, it doesn't seem to work. However, accessing URLs on my own domain works perfectly fine. I've heard about a workaround involving PHP and setting up ...

Is there a maximum file size limit for uploads in jQuery when using ajax requests?

I'm curious if there's a size limit for jQuery uploading (via Ajax)? Specifically, I'm working on a form to upload multiple images to the server (currently using WampServer locally). So far, I've been able to easily upload one, two, or ...

Looking for a list that you can edit with CSS styling?

I'm struggling to create a flexible list that can be easily modified. My goal is to have an ordered list with unique labels such as 1,2,3,4,5,6,7,8,9,0,A,B,!,# Although I've managed to number the list items, I am stuck on adding the extra label ...

Using dots instead of lines for the carousel indicators in PrimeNG

I'm currently working on a carousel feature, but I want to change the indicators from lines to small dots. I know the solution lies within the CSS files, but I'm not sure how to implement it. I think I need to create a new CSS class, but I could ...

Exploring the functionalities of scss and minified files within Visual Studio 2017

I have very little knowledge of scss. In our Visual Studio 2017 solution explorer, there is a .scss file called Theme6.scss. When I expand it, I see a file named Theme6.css. Upon expanding that file, two more files are revealed: Theme6.css.map and Theme6. ...

Identify the class within a child division and include the class in a separate division

How can I detect a special class within a child div and then add a class to another div when that specific class is found? For example: <ul class="m-a-ul"> <li class="menu"> </ul> Now, if the class collapsed is dynamically added: ...

Is there a way to prevent the Pace js plugin from running on page load, but still have it execute during Ajax requests only?

I have successfully implemented the jquery pace plugin with a progress bar theme. Everything is working well, but I am looking to make it run only on ajax requests. I have tried various solutions found through research, but haven't had any luck. Belo ...

What could be causing the closest() method in my JavaScript code to not locate the nearest class?

I've encountered an issue while dynamically creating new classes with input fields in my project. For some reason, when I try to remove a previous row, nothing happens. Can anyone help me troubleshoot this problem? Here is the JavaScript code that I ...

The CSS infinite animation becomes erratic and sluggish after a short period of time

My website featured a banner with a series of thumbnail images that animated at different intervals and looped indefinitely. Initially, the animations ran smoothly, but after a few seconds, they began to slow down and lose consistency. To troubleshoot, I u ...

Does running npm install automatically compile the library code as well?

I have a query regarding npm and its functionality. I also posted the same question on Reddit, but haven't received a satisfying answer yet. Let's use the jQuery npm package as a case study. Upon running the command npm install jquery, I notic ...

Utilize the jQuery queue method to trigger an action following the completion of a toggleClass

I am working on a sliding content element that should display its contents when clicked. Below is the code snippet: <div class="boxwrap hidden-sm hidden-xs"> <div class="boxright pull-left"> <a href="#" id="toggleContactOpen" class="pull ...

What HTML template is your go-to when starting a new project?

Is this the ultimate solution for creating a basic "Hello World" HTML template? I have been experimenting with this starter code to develop simple websites. Are there any other interesting tags I could incorporate to enhance it further? Usually, I refer ...

The background color of social media links spills over onto other links, creating a messy overlap

I have been trying to confine the teal hover effect within the boundaries of my social media links. Despite adjusting padding, heights, and widths using CSS properties, the hover effect still extends beyond the right border. Here is how it appears without ...

Using JQuery to send files via $.ajax

Possible Duplicate: jQuery Ajax File Upload I'm looking to implement a file upload feature using PHP and display a loader to the user during the upload process. I want to use jQuery to update a specific div after the upload is complete. However, ...