Path: csiph.com!feeder.erje.net!1.eu.feeder.erje.net!news.mb-net.net!open-news-network.org!.POSTED.180.224.197.178.dynamic.wless.zhbmb00p-cgnat.res.cust.swisscom.ch!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Re: Altering a CSS class with JavaScript? Date: Fri, 01 Jun 2018 13:25:57 +0200 Organization: PointedEars Software (PES) Lines: 121 Message-ID: <3197134.joGGxDtPI3@PointedEars.de> References: <1df34733-dc9a-4edf-a4b8-92dc1de90a16@googlegroups.com> <233c3ea9-b903-44ad-8435-2a6236003a38@googlegroups.com> Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit Injection-Info: gwaiyur.mb-net.net; posting-host="180.224.197.178.dynamic.wless.zhbmb00p-cgnat.res.cust.swisscom.ch:178.197.224.180"; logging-data="11117"; mail-complaints-to="abuse@open-news-network.org" User-Agent: KNode/4.14.2 X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. Michael Haufe (TNO) wrote: >> Dr J R Stockton wrote: >> > I have a long page containing numerous HTML elements >> > of class ZYZ. >> > >> > On page load complete, class XYZ is {display: block} or >> > not defined. >> > >> > At a certain moment, I wish class XYZ to be {display: none}. >> > >> > At a later moment, I wish class XYZ to be {display: block}. >> > >> > What JavaScript do I need to alter the value of the display >> > property of a class? >> > >> > How can JavaScript create the initial XYZ? >> > >> > The page must be redrawn after the change, preferably just one >> > redraw per change to XYZ. >> >> My first reaction is that you are approaching it in potentially a less >> than optimal manner. That was my second reaction. My first one was “Google is your friend!”. >> Here is an impressionistic example of my suggestion: >> >> >> >>
Hello World
>> >> For more than one-time access, it is more efficient to create and use a selector-to-rule map: As a starting point: let selectorToRule = new Map(); Array.from(document.styleSheets).forEach((sheet) => { Array.from(sheet.cssRules || sheet.rules).forEach((rule) => { let selectorText = rule.selectorText; selectorToRule.set(selectorText, selectorToRule.has(selectorText) ? selectorToRule.get(selectorText).push(rule) : [rule]); }); }); selectorToRule.get(".xyz")[0].style.display = 'block'; [Thanks for reminding me of ECMAScript 2015’s Array.from(); so unbounded .slice.call() should be a thing of the past now. JFTR, I added an Array.from() that uses Array.prototype.map() to JSX:object.js on 2013-09-04 :) ] The most simple, compatible, and arguably most efficient, way to modify a stylesheet is to append a new “link” or “style” element to the “head” element. (However, I am not certain if it works with “link” elements, too.) -- PointedEars FAQ: | | Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.