Change CSS class properties with jQuery
You can't change CSS properties directly with jQuery. But you can achieve the same effect in at least two ways.
Dynamically Load CSS from a File
function updateStyleSheet(filename) {
newstylesheet = "style_" + filename + ".css";
if ($("#dynamic_css").length == 0) {
$("head").append("<link>")
css = $("head").children(":last");
css.attr({
id: "dynamic_css",
rel: "stylesheet",
type: "text/css",
href: newstylesheet
});
} else {
$("#dynamic_css").attr("href",newstylesheet);
}
}
The example above is copied from:
- How To Switch CSS Files On-The-Fly Using jQuery
Dynamically Add a Style Element
$("head").append('<style type="text/css"></style>');
var newStyleElement = $("head").children(':last');
newStyleElement.html('.red{background:green;}');
The example code is copied from this JSFiddle fiddle originally referenced by Alvaro in their comment.
How to change a property of a CSS class dynamically using javascript (no JQuery)
I don't know what triggers your animation, but let's say it's a click
on each of the .sbox
elements.
As you can't change the CSS, you can instead use the script to add an inline style height using .style.height
.
Here is a snippet:
var sboxes = document.querySelectorAll(".sbox");
sboxes.forEach(function(box, index){ box.onclick = function(){ box.style.height = "360px"; box.className = 'sboxopen'; }})
.sbox { height: 0px; transition: height 1s ease-out; overflow: hidden; border: 8px solid gray; /* Added for better visibility */}
.sboxopen { height: 130px; transition: height 1s ease-out; overflow: hidden; border: 8px solid gray; /* Added for better visibility */}
<div class='sbox'>Box 1</div><br><div class='sbox'>Box 2</div><br><div class='sbox'>Box 3</div>
change CSS class on the fly using Jquery
Dz1 Answered
From what you just said, use "removeClass" to remove the one you want
to remove, then "addClass" to add the one that will change the
background. –
javascript modify css class property while knowing only the class' name
Following your response in the comment, if the element is being generated by Jquery, then the library is most likely installed. Here is something you can try to select it via Jquery and change the require property.
$(document).ready( function(){
$('.my-class-name').css('display', 'block');
});
Substituting 'block' for whatever setting you require.
If Jquery is included it should do what your require on page load. You can also attach it to other events as well.
$(document).ready(function(){
$('.my-class-name').click(classClicked);
})
function classClicked(){
$(this).css('display','block')
}
how to dynamically change the property of css using jquery
That's because you pass to css
method an object containing property
property. In this case it's better to pass two arguments to css
method:
function changeProperty(mine , property , value){
$(mine).css(property, value);
}
However, if you want to pass an object, you can dynamically build it:
function changeProperty(mine , property , value){
var obj = {};
obj[property] = value;
$(mine).css(obj);
}
Another improvement is to use this
value inside of the function (that is the same with mine
in this case):
function changeProperty(property , value){
$(this).css(...);
}
Then in HTML you will have changeProperty.call(this, "font", "Arial")
.
.css( propertyName, value )
Set one or more CSS properties for the set of matched elements.
propertyName
: A CSS property name.Type:
String
value
: A value to set for the property.Type: String or Number
Related Topics
How to Use External ".Js" Files
Does Ie8 Out-Of-The-Box Have Support For 'Localstorage'
PHP+Js: How to Do Fileuploads in HTML Form as Content-Type Multipart (Via Js)
How to Make an HTML Text Box Show a Hint When Empty
How to Save a Base64 Image to User'S Disk Using JavaScript
Why Is ≪Marquee≫ Deprecated and What Is the Best Alternative
How to Display a JavaScript Var in HTML Body
Stop an Input Field in a Form from Being Submitted
How to Get Number of Rows in ≪Textarea ≫ Using JavaScript
How to Escape Quotes in HTML Attribute Values
How to Get the Position of Text Within an Element
How to Update Placeholder Color Using JavaScript
Textarea That Can Do Syntax Highlighting on the Fly
React.Js: Set Innerhtml VS Dangerouslysetinnerhtml
Best Way to Detect That Html5 ≪Canvas≫ Is Not Supported