I've got a class with the display set to none
I'd like to in Javascript now set it to inline
I'm aware I can do this with an id
with getElementById
but what's the cleanest way to do it with a class?
-
3Just for future reference, because I think it closely resembles what you want: hunlock.com/blogs/Totally_Pwn_CSS_with_JavascriptZirak– Zirak2011年04月22日 08:41:38 +00:00Commented Apr 22, 2011 at 8:41
-
@Zirak Unfortunately, that link is brokenHungryBeagle– HungryBeagle2021年02月05日 19:51:29 +00:00Commented Feb 5, 2021 at 19:51
8 Answers 8
You can do that — actually change style rules related to a class — using the styleSheets
array (MDN link), but frankly you're probably better off (as changelog said) having a separate style that defines the display: none
and then removing that style from elements when you want them no longer hidden.
Do you mean something like this?
var elements = document.getElementsByClassName('hidden-class');
for (var i in elements) {
if (elements.hasOwnProperty(i)) {
elements[i].className = 'show-class';
}
}
Then the CSS
.hidden-class { display: none; }
.show-class { display: inline; }
-
Claims there's no such method as
getElementsByClassName
fordocument
Skizit– Skizit2011年04月22日 08:49:53 +00:00Commented Apr 22, 2011 at 8:49 -
2Hrm, it's not supported by older IE. I'd honestly recommend something like jQuery, but get bashed every time I suggest it.Markus Hedlund– Markus Hedlund2011年04月22日 08:54:22 +00:00Commented Apr 22, 2011 at 8:54
-
1@Zharkus: No harm in recommending using a library as an adjunct or postscript to an answer that doesn't require one (said one of the bashers); it's when someone answers a non-jQuery question with a purely jQuery answer that it's in appropriate. @Skizit: A library ilke jQuery, Prototype, YUI, Closure, or any of several others can smooth over browser differences for you, and provide lots of useful utilities.T.J. Crowder– T.J. Crowder2011年04月22日 09:19:49 +00:00Commented Apr 22, 2011 at 9:19
-
@Skizit: You can add
getElementsByClassName
to browsers that don't support it: ejohn.org/blog/getelementsbyclassname-speed-comparisonT.J. Crowder– T.J. Crowder2011年04月22日 09:23:21 +00:00Commented Apr 22, 2011 at 9:23
You can use getElementsByClassName
in which you'll get an array of elements. However this is not implemented in older browsers. In those cases getElementsByClassName
is undefined
so the code has to iterate through elements and check which ones have the desired class name.
For this you should use a javascript framework such as jQuery, mootools, prototype, etc.
In jQuery it could be done with a one-liner as this:
$('.theClassName').css('display', 'inline')
-
1Wrong, that should be
$('.theClassName').css('display', 'inline')
Markus Hedlund– Markus Hedlund2011年04月22日 08:50:27 +00:00Commented Apr 22, 2011 at 8:50
you can create new style rule instead.
var cssStyle = document.createElement('style');
cssStyle.type = 'text/css';
var rules = document.createTextNode(".YOU_CLASS_NAME{display:hidden}");
cssStyle.appendChild(rules);
document.getElementsByTagName("head")[0].appendChild(cssStyle);
$("#YOUR_DOM_ID").addClass("YOUR_CLASS_NAME");
You may like to exploit/rewrite this function:
function getStyleRule(ruleClass, property, cssFile) {
for (var s = 0; s < document.styleSheets.length; s++) {
var sheet = document.styleSheets[s];
if (sheet.href.endsWith(cssFile)) {
var rules = sheet.cssRules ? sheet.cssRules : sheet.rules;
if (rules == null) return null;
for (var i = 0; i < rules.length; i++) {
if (rules[i].selectorText == ruleClass) {
return rules[i].style[property];
//or rules[i].style["border"]="2px solid red";
//or rules[i].style["boxShadow"]="4px 4px 4px -2px rgba(0,0,0,0.5)";
}
}
}
}
return null;
}
- to scan all style sheets attached pass "" as third argument, otherwise something like "index.css"
- ruleClass contains starting '.'
- if (rules[i].selectorText && rules[i].selectorText.split(',').indexOf(property) !== -1) condition improvement found here https://stackoverflow.com/a/16966533/881375
- don't forget to use javascript syntax over css properties, e.g. box-shadow vs. boxShadow
-
2finally, a good answer:) I edited it and added a "value" input to immediately change the ruleguy_m– guy_m2015年02月10日 17:15:14 +00:00Commented Feb 10, 2015 at 17:15
-
This was the answer for me as well. Changing the styles of the elements didn't work because some elements are added to the dom via media query (after the event when elements were changed). I'm on a React JS project so the CSS is transpiled into the html. So I had to kill the test for href.Mike– Mike2019年08月08日 01:03:27 +00:00Commented Aug 8, 2019 at 1:03
Although this is long gone, here a few remarks:
Using
display: inline
to make things visible again may spoil the page flow. Some elements are displayed inline, others block etc. This should be preserved. Hence, only define a .hidden style and remove it to make things visible again.How to hide: There are (at least) two ways to hide elements, one is the above mentioned
display: none
which basically makes the element behave as if it was not there, and thevisibility: hidden
which renders the element invisible but keeps the space it occupies. Depending on what you want to hide, the visibility may be a better choice, as other elements will not move when showing/hiding an element.Adding/removing classes vs. manipulating CSS rules: The result is quite different. If you manipulate the CSS rules, all elements having a certain CSS class are affected - now and in the future, i.e. new elements dynamically added to the DOM are also hidden, whereas when you add/remove a class, you must make sure that newly added elements also have the class added/removed. So, I'd say adding/removing classes works well for static HTML, whereas manipulating CSS rules might be a better choice for dynamically created DOM elements.
To change CLASS you need to edit document stylesheets
[...document.styleSheets[0].cssRules].find(x=> x.selectorText=='.box')
.style.display='inline';
[...document.styleSheets[0].cssRules].find(x=> x.selectorText=='.box')
.style.display='inline';
.box {
margin: 10px;
padding: 10px;
background: yellow;
display: none
}
<div class="box" >My box 1</div>
<div class="box" >My box 2</div>
<div class="box" >My box 3</div>
Best way to do it is to have a hidden class, like so:
.hidden { display: none; }
After that, there is a className
attribute to every element in JavaScript. You can just manipulate that string to remove occurrences of the hidden
class and add another one.
One piece of advice: Use jQuery. Makes it easier to deal with that kind of stuff, you can do it like:
$('#element_id').removeClass('hidden').addClass('something');
-
2Pfft! What's not Javascript about jQuery? ;-)changelog– changelog2011年04月22日 08:44:42 +00:00Commented Apr 22, 2011 at 8:44
-
9The fact that jQuery-specific code doesn't work in plain-vanilla JS, if jQuery isn't included? "Use jQuery" isn't always a valid answer to every question about JS, and i'm sure some people get tired of hearing it.cHao– cHao2011年04月27日 11:51:31 +00:00Commented Apr 27, 2011 at 11:51
-
2Generally your answer shouldn't introduce jQuery if it wasn't mentioned in the question. If you really want to include jQuery, should answer in vanilla JavaScript and add a "...however, in jQuery, it would look like this:"2011年04月27日 15:49:42 +00:00Commented Apr 27, 2011 at 15:49
-
2If you bothered reading the answer, you'd see that I go and explain how to do it in vanilla JavaScript, and then, and only then do I recommend jQuery.changelog– changelog2011年04月28日 08:27:37 +00:00Commented Apr 28, 2011 at 8:27
-
The only JS i see in the answer is jQuery-specific. If you'd shown an example of how to do it otherwise, i'd probably have upvoted the answer. (I didn't downvote, but mostly because i feel -1 is enough of a point.)cHao– cHao2011年05月02日 23:48:55 +00:00Commented May 2, 2011 at 23:48