Adding elements to an existing CSS class can be accomplished by using the class attribute

Can I modify my existing CSS class by adding new elements?

CSS:

.usrgrid {
    color:blue;
    background-color:white;
}

Is there a way to add attributes to the .usergrid class?

I attempted using

$(.usergrid).css("display","block");
but it didn't work.

Thank you.

 $(".usergrid .k-button").css({ "display": "block" });
.usergrid .k-button{
  color:black;
  background-color:white;
}
<!DOCTYPE html>
<html>
<head>
   <link rel="stylesheet" href="//kendo.cdn.telerik.com/2016.3.914/styles/kendo.common-bootstrap.min.css" />
    <link rel="stylesheet" href="//kendo.cdn.telerik.com/2016.3.914/styles/kendo.bootstrap.min.css" />
  <link rel="stylesheet" href="//kendo.cdn.telerik.com/2016.3.914/styles/kendo.bootstrap.mobile.min.css" />
     <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script src="http://kendo.cdn.telerik.com/2016.3.914/js/kendo.all.min.js"></script>

    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script src="http://kendo.cdn.telerik.com/2016.2.607/js/kendo.all.min.js"></script>

  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>

</body>
</html>

Answer №1

The issue at hand is that modifying parsed CSS directly is not possible. You can only add inline styles to a specific element. For example, if you have a <div class="usrgrid">, the only way to modify it is like this:

<div class="usrgrid" style="color: red">
. The examples mentioned in this post using .css function follow this concept.

Other alternatives include:

  1. Dynamically adding CSS code
  2. Loading CSS file dynamically

Additional Information

I now understand where the problem arises from. When you have an element and wish to make modifications to it. For instance, changing its visibility or background color. The crucial aspect here is having your initial state and a "modifier" for it. Removing the modifier will revert back to the original state. From a CSS standpoint, this translates to:

.my-elem {}

.my-elem--big {}

.my-elem--yellow {}

.my-elem--visible {}

In your JS, all you need to do is add the appropriate set of modifiers (classes) to your element.

<div class="my-elem"></div>

<div class="my-elem my-elem--visible"></div>

There exists a naming convention in CSS known as BEM, which effectively addresses this issue. Simply put, it divides your UI into blocks, elements, and modifiers.

Following this methodology helps in separating concerns (logic of when an element should be visible handled by JS, and what it means for it to be visible managed by CSS) making your code more organized and predictable.

Personally, I always steer clear of mixing CSS with JS. Even for something as simple as display: block. These two aspects serve different purposes and should remain separate.

Answer №2

Give this snippet a try

  $('.usergrid').show(); 

Answer №3

To apply various CSS properties at once, you can utilize the syntax shown below:

$('.profile-grid').css({"display": "flex", "background-color": "#f9f9f9"}); 

Answer №4

<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script>
jQuery(document).ready(function(){
   jQuery('.usergrid').css("display","block"); 
});
</script>

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

Rendering an Angular page with data using Node.js

I have a scenario for my website where users need to input a URL with a parameter (an ID) and receive back the corresponding page containing information from the database. I am using Angular4 but I am unsure of how to achieve this. It's straightforwa ...

Unable to locate a React component module that has been published

After successfully publishing a React component to NPM, I encountered an issue when trying to use it in another project - I couldn't find the module! Module not found: Can't resolve 'react-subreddit-posts' in '/Users/kyle.calica/C ...

Modifying Props in Reactjs: Ways to update data passed from parent component to child component

Currently, I am working on a project where I have multiple components on a page and pass data between them using props. The issue arises when I update the data in the parent component but the child component still uses the old data. Let me illustrate with ...

There seems to be an error with the return from the `mapDispatchToProps` function in `

I've been working on getting my v6 redux-form to render in the App component in order to display it in the browser. However, I keep encountering this error: Uncaught Error: `mapDispatchToProps` must return an object. Instead received function () { ...

"JIRA post functionality not functioning correctly on Internet Explorer and Firefox due to jQuery compatibility

I am facing an issue while trying to submit data to JIRA. It seems to be working fine in Chrome but not in IE/FF. I suspect there is something wrong with the ajax call setup, but I'm not entirely sure what needs to be done to fix it. $(&apos ...

A small space underneath the <a> element nested within a <li> tag within a navigation bar

As a new programmer, I am currently working on creating a navbar. However, I have encountered an issue where there are small gaps underneath the user when my cursor hovers over it. There is a white gap under the user element. I expect that the hover back ...

I am looking to record the exact date and time when employees clock in and out, and store this information in a MySQL

To track employee clock-ins and clock-outs, I have created a MySQL table named emp_log. When an employee clicks a button, the current date and time are automatically inserted into the emp_log table, along with their user_id and clock_id. This process shou ...

Sinon.js: How to create a mock for an object initialized with the new keyword

Here is the code that I am working with: var async = require('async'), util = require('util'); var Parse = require('parse/node'); function signup(userInfo, callback) { var username = userInfo.username, email ...

Having trouble populating the dropdown menu with data retrieved from the database

I'm having an issue fetching values from the database to populate a drop-down list using jQuery. Unfortunately, nothing is displaying in the dropdown list. Below is the code snippet: getlist.php <?php $conn =mysqli_connect("localhost&q ...

arrange a div inside another div

I'm struggling to grasp the concept of how divs function in HTML. Below is a snippet of my HTML code: <div id="user_p"> <img src="img/pp/djbaptou.jpg"> <div class="followings"> djbaptou </br> Baptiste Arnaud </br> ...

The alignment of content in the <a> tag is not centered within a <ul> list item (horizontal menu)

I am new to web development and have recently started creating my first webpage using HTML and CSS. I'm still learning and don't have a strong understanding yet. One issue I'm facing is with centering elements on the page. I have tried usin ...

What is the process for transitioning from Ajax to Fetch API in JavaScript?

Currently, I am utilizing the JavaScript version of RiveScript which relies on ajax, and I have decided to move away from using jQuery. There is a single line of ajax code that I need to update to integrate the new Fetch API. **Note: The ajax code can be ...

Attempting to utilize a custom element, the message "You attempted to use polymer without loading it first" confirms that it has indeed been imported

I've been experimenting with a custom element for a paper-dialog popup that I want to incorporate. Surprisingly, when I create a test page within the same file where the custom element is defined, using all the identical imports as my main index, it w ...

Codeigniter code to retrieve dynamic data in a select box

I am trying to implement a functionality where selecting an option from one dropdown will dynamically populate the options in another dropdown based on the selection. I believe I need to use an onchange function, but I'm unsure how to retrieve data fr ...

Tips for transferring PHP data to a JavaScript script within WordPress by making use of AJAX

After experimenting with both wp_localize_script and wp_add_inline_script, I am still unable to achieve the desired outcome. In my plugin's PHP file (which is enqueued with wp_enqueue_scripts elsewhere), I have the following code: wp_register ...

Tips for ensuring only digits can be entered in a textbox on Firefox

Having trouble validating digits in a textbox on both IE 9 and Firefox versions 4 & 5. Does anyone know how to solve this issue? I've attempted the solutions provided in previous questions, but I'm still encountering problems. My goal is to only ...

Using Javascript to update text within HTML elements without affecting the structure for upcoming event listeners

I've been working on a piece of code that identifies a specific string of characters (_aff.tractionsCode) and swaps it out with another string (tractionsId). The implementation looks something like this: content = document.body.innerHTML.replace(_aff ...

What is the best way to showcase additional fields from a custom post type along with the featured thumbnail feature?

'I designed a custom post plugin in Wordpress that successfully displays the title and main content, but I am facing issues with displaying other fields such as company details. Despite mentioning a featured thumbnail option in my plugin, I am not ab ...

Tips for effectively utilizing tabs to showcase information for a particular user rather than solely the initial one

I am fetching multiple user data from an API call, and all users are rendered using the same HTML and CSS. However, when I switch between tabs for each user, only the first user's information changes instead of the specific user I clicked on. I have a ...

Error: Query has already been processed: Updating Todo with ID "612df063a8f"

After updating mongoose to the latest version (6.0.2), I encountered an error that crashes the application whenever .updateOne() is executed. However, the object is still updated inside the database. Below is my code snippet: async(req,res) => { a ...