This question already has answers here:
How can I remove a style added with .css() function?
(21 answers)
Closed 6 years ago.
Let's say I have a div.foo with this CSS:
.foo {
height: 70vh;
}
And I change the height of div.foo with jQuery like this:
$('.foo').height(100);
How do I reset the CSS of div.foo so its height is rendered 70vh again?
One solution that works is to reload the entire CSS file this way. But I wonder if there is a solution to only reset the CSS of div.foo, not the entire stylesheet?
With jQuery, you are adding your styles to the style attribute of the element. It is removed again, if you call the function again with an empty string (""). This will remove the property in the style attribute.
If you have e.g. this command:
$('.foo').css('height', '100px');
You can revoke it by
$('.foo').css('height', '');
See also the discussion here: https://stackoverflow.com/a/4036868/3233827
$('.foo).height(''); should be sufficient to erase the style applied by jQuery
You can remove the style attribute from the element. This attribute holds any dynamic styles.
$('.foo').css('background', 'teal');
$('#reset').click(function() {
$('.foo').removeAttr('style');
});
.foo {
height:100px;
width:100px;
background:red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<div class="foo"></div>
<p><button id="reset">Reset style</button></p>
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.
I have a CSS file that specifies an :after style against a class ('view-performance')
Using jQuery (because my JS file contains the business logic), how can I remove the :after style based on the business logic?
You cannot reach pseudo elements using jQuery!
But you can do it with css, and manipulate it using .addClass() / .removeClass()
Here is an example how can you remove pseudo element :after by adding css class:
Your JS:
$('.view-performance').addClass('without-after-element');
Your CSS:
.view-performance:after {
content: 'i go after';
}
.view-performance.without-after-element:after {
content: none;
}
You may also check this question:
stackoverflow.com/questions/17788990/access-the-css-after-selector-with-jquery
$('.view-performance:after').css('display','none');
I have a Jquery selectmenu called #Main which implicitly gets a #Main-button. When I try to set #Main-button's width using css as
#Main-button {
width:200px;
}
it has no effect.
When I explicitly set
$( "#Main" ).selectmenu({ width:200})
it has the desired effect and under Firebug I see that it has appended a style="width:200" on the #Main-button, which is what I tried using CSS at the first place.
What is different? I've checked that the my CSS style sheet gets called AFTER the Jquery one, so there is no precedence issue
Also I notice that html elements turned to Jquery elements cannot be styled using CSS targeted at the specific element, even with the use of unique id's,but require the use of Jquery classes like .ui-menu etc
why do they behave differently? are there any specific styling gudilines when Jquery is involved?
In your CSS you've
#Main-button {
width:200px;
}
but the JS is adding dynamic inline style based on content. So it's having style attribute.
So in terms of CSS specificity their CSS beats you.
You must use !important in your rule to avoid overriding of your CSS.
#Main-button {
width:200px !important;
}
To style selectmenu or every jquery widget, you need to use jquery default classes. Your selector must be like
#Main-button.ui-selectmenu-menu li a
And also you can extend _renderItem and _renderMenu functions of selectmenu for different styling.
This question already has answers here:
Selecting and manipulating CSS pseudo-elements such as ::before and ::after using javascript (or jQuery)
(26 answers)
Closed 8 years ago.
My CSS looks like:
/* works */
.badger-left:after {
left: 0;
border-radius: 6px 0 6px 0;
background: #428bca;
}
jQuery to change it on the fly:
$('.badger-left:after').css('background-color', "red"); // doesnt work
$('.badger-left').attr({ 'data-badger': "USA" }); // works
HTML:
<div class="col-sm-12 badger-left" data-badger="">
</div>
Why isn't the jQuery selector working and what should I do to make it work?
http://screencast.com/t/onWs4KDJVD -- where it says "USA" (USA is set from the attr. data-badger in jquery), that background needs to change to red (or whatever color specified) via jQuery.
Thanks
The CSS selectors :after and :before is not supported in javascript, most pseudo-classes are not.
jQuery adds the css inline. Since :before, and :after do not really exist there is no style attribute to place the css into anyway.
Run this test to see:
$("*:after") // returns []
$("*") // returns a huge array ;)
You could use either a jQuery .append() or .add() to achieve a similar effect.
The CSS ::after selector just adds an "imaginary" div to the inside of the element it's applied to just before the closing tag.
.append()
https://api.jquery.com/append/
.add()
https://api.jquery.com/add/
jQuery:
$('.badger-left').append('div');
CSS:
.badger-left div{
/* ".badger-left::after" styling would go here */
}
This question already has answers here:
Selecting and manipulating CSS pseudo-elements such as ::before and ::after using javascript (or jQuery)
(26 answers)
How to update placeholder color using Javascript?
(5 answers)
Closed 2 years ago.
Is it possible to change a CSS pseudo-element style via JavaScript?
For example, I want to dynamically set the color of the scrollbar like so:
document.querySelector("#editor::-webkit-scrollbar-thumb:vertical").style.background = localStorage.getItem("Color");
and I also want to be able to tell the scrollbar to hide like so:
document.querySelector("#editor::-webkit-scrollbar").style.visibility = "hidden";
Both of these scripts, however, return:
Uncaught TypeError: Cannot read property 'style' of null
Is there some other way of going about this?
Cross-browser interoperability is not important, I just need it to work in webkit browsers.
If you're comfortable with some graceful degradation in older browsers you can use CSS Vars. Definitely the easiest of the methods I've seen here and elsewhere.
So in your CSS you can write:
#editor {
--scrollbar-background: #ccc;
}
#editor::-webkit-scrollbar-thumb:vertical {
/* Fallback */
background-color: #ccc;
/* Dynamic value */
background-color: var(--scrollbar-background);
}
Then in your JS you can manipulate that value on the #editor element:
document.getElementById("#editor").style.setProperty('--scrollbar-background', localStorage.getItem("Color"));
Lots of other examples of manipulating CSS vars with JS here: https://eager.io/blog/communicating-between-javascript-and-css-with-css-variables/
To edit an existing one which you don't have a direct reference to requires iterating all style sheets on the page and then iterating all rules in each and then string matching the selector.
Here's a reference to a method I posted for adding new CSS for pseudo-elements, the easy version where you're setting from js
Javascript set CSS :after styles
var addRule = (function (style) {
var sheet = document.head.appendChild(style).sheet;
return function (selector, css) {
var propText = typeof css === "string" ? css : Object.keys(css).map(function (p) {
return p + ":" + (p === "content" ? "'" + css[p] + "'" : css[p]);
}).join(";");
sheet.insertRule(selector + "{" + propText + "}", sheet.cssRules.length);
};
})(document.createElement("style"));
addRule("p:before", {
display: "block",
width: "100px",
height: "100px",
background: "red",
"border-radius": "50%",
content: "''"
});
sheet.insertRule returns the index of the new rule which you can use to get a reference to it for it which can be used later to edit it.
EDIT: There is technically a way of directly changing CSS pseudo-element styles via JavaScript, as this answer describes, but the method provided here is preferable.
The closest to changing the style of a pseudo-element in JavaScript is adding and removing classes, then using the pseudo-element with those classes. An example to hide the scrollbar:
CSS
.hidden-scrollbar::-webkit-scrollbar {
visibility: hidden;
}
JavaScript
document.getElementById("editor").classList.add('hidden-scrollbar');
To later remove the same class, you could use:
document.getElementById("editor").classList.remove('hidden-scrollbar');
I changed the background of the ::selection pseudo-element by using CSS custom properties doing the following:
/*CSS Part*/
:root {
--selection-background: #000000;
}
#editor::selection {
background: var(--selection-background);
}
//JavaScript Part
document.documentElement.style.setProperty("--selection-background", "#A4CDFF");
You can't apply styles to psuedo-elements in JavaScript.
You can, however, append a <style> tag to the head of your document (or have a placeholding <style id='mystyles'> and change its content), which adjusts the styles. (This would work better than loading in another stylesheet, because embedded <style> tags have higher precedence than <link>'d ones, making sure you don't get cascading problems.
Alternatively, you could use different class names and have them defined with different psuedo-element styles in the original stylesheet.
I posted a question similar to, but not completely like, this question.
I found a way to retrieve and change styles for pseudo elements and asked what people thought of the method.
My question is at Retrieving or changing css rules for pseudo elements
Basically, you can get a style via a statement such as:
document.styleSheets[0].cssRules[0].style.backgroundColor
And change one with:
document.styleSheets[0].cssRules[0].style.backgroundColor = newColor;
You, of course, have to change the stylesheet and cssRules index. Read my question and the comments it drew.
I've found this works for pseudo elements as well as "regular" element/styles.
An old question, but one I came across when try to dynamically change the colour of the content of an element's :before selector.
The simplest solution I can think of is to use CSS variables, a solution not applicable when the question was asked:
"#editor::-webkit-scrollbar-thumb:vertical {
background: --editorScrollbarClr
}
Change the value in JavaScript:
document.body.style.setProperty(
'--editorScrollbarClr',
localStorage.getItem("Color")
);
The same can be done for other properties.
Looks like querySelector won't work with pseudo-classes/pseudo-elements, at least not those. The only thing I can think of is to dynamically add a stylesheet (or change an existing one) to do what you need.
Lots of good examples here:
How do I load css rules dynamically in Webkit (Safari/Chrome)?