How to change style of a class inside Javascript? [duplicate] - javascript

This question already has answers here:
jquery to change style attribute of a div class
(9 answers)
Closed 7 years ago.
I want to change background color of my div using jQuery selector and I can't get it to work.
My Codepen.
HTML
<div class='demo'></div>
CSS
.demo {
height: 100px;
width: 100px;
}
JS
$(document).ready(function() {
$('demo').style.backgroundColor = "black";
});

$('demo') will return jquery object not a dom object also your selector is missing a ., use
$('.demo').css('background-color ','black');
Reference: jquery css() doc.

Related

Get CSS properties in JS by css class name [duplicate]

This question already has answers here:
How to get a style attribute from a CSS class by javascript/jQuery?
(8 answers)
Closed 2 years ago.
How do I get the CSS properties of an element with js? Let say I have the following
.btn {
background-color: green;
}
.navbtn {
font-size: 20px;
}
and
<button id='mybtn' class='btn navbtn' onclick='alert("hi")'>Click me</button>
Then, I want to extract the CSS properties of the element mybtn. If I do
window.getComputedStyle(document.getElementById('mybtn'))
it returns a CSSStyleDeclaration which contains all CSS properties, even those I didn't set. However, what I want is an object (or whatever) that contains
{
background-color: green;
font-size: 20px;
}
and everything else will consider as 'default'.
So my question is:
Is there such function or library that does the job?
If not, is it possible to get the CSS properties given the class name (like getProperties('btn') returns { background-color: green; })
Thanks in advance
So simple as this code :
let background = document.querySelector('.btn').style.backgroundColor;
console.log(backround);

Javascript code for change css width of a class not working [duplicate]

This question already has answers here:
What do querySelectorAll and getElementsBy* methods return?
(12 answers)
getElementsByClassName() is not working [duplicate]
(2 answers)
Closed 5 years ago.
The simple code for change width of a class in css code using javascript is not working. I have given the width is 200px. Then i want to change the width to 50px. i want to use a javascript code to change width. But it is not working.
document.getElementsByClassName("main").style.width = "50px";
.main {
width: 200px;
height: 200px;
background-color: red;
}
<div class="main"></div>
fiddle: https://jsfiddle.net/33zc0efq/
document.getElementsByClassName returns an array of object so you need to specify index for that. Change it to document.getElementsByClassName("main")[0].style.width="50px";
document.getElementsByClassName("main")[0].style.width="50px";
.main{
width:200px;
height:200px;
background-color:red;
}
<div class="main">
</div>

jQuery change values in a class [duplicate]

This question already has answers here:
Change CSS class properties with jQuery
(8 answers)
Changing CSS properties via JavaScript
(6 answers)
Closed 5 years ago.
Is there a way to use jQuery to set values inside a class? So for example if I have a class like this:
.menubar {
background: #333;
color: #fff;
}
Can I then run something like:
$('.menubar').css('background', '#999');
... to set the value of that class to #999? And I don't mean to select all the elements with the class and set their background, I mean can I change the actual classes value to #999 so that if an element is created later with the class it will have #999 instead of #333.

Why can't I create a div that has more than one style element? [duplicate]

This question already has answers here:
Issue while setting html of div using the jquery html() method
(5 answers)
Closed 6 years ago.
I am trying to create div like so using jQuery:
let gridBlock = $("<div style=width:10px; height:20px; background-color:red;></div>");
However when I look at the render page the console shows that the div only has a style of width:
What am I doing wrong?
Though you have already got the answer in comment but there is a better way to implement using class. The reason css-specificity. Adding style using style attribute is hard to overwrite & need to use !important for that.
So to avoid that you can use the class attribute and define the style in css file
some.css
.someClass {
width: 10px;
height: 20px;
background-color: red;
}
in js file
let gridBlock = $("<div class='someClass'>Hello</div>");
DEMO
Use quotes mate
let gridBlock = $("<div style='width:10px; height:20px; background-color:red;'></div>");

Jquery Select css Class :before [duplicate]

This question already has answers here:
Selecting and manipulating CSS pseudo-elements such as ::before and ::after using javascript (or jQuery)
(26 answers)
Closed 6 years ago.
How can I select an css class in jquery (or javascript), if it looks like this:
.funkyradio input[type="checkbox"]:checked ~ label:before {}
So far I got this:
$('.funkyradio[type=checkbox]').click(function() {
But I do not know how to further approach.
Try this : Assuming funkyradio is the css class name. Put space between funkyradio and [type=checkbox] as checkbox is a child of funkyradio-success
$('.funkyradio [type=checkbox]').click(function() {

Categories