Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #31673

Re: How to replace a class?

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: How to replace a class?
Date 2016-11-10 23:11 +0100
Organization PointedEars Software (PES)
Message-ID <20855761.ouqheUzb2q@PointedEars.de> (permalink)
References <5a64eafc-a7f3-44e2-88ce-fe388101ecba@googlegroups.com> <XnsA6BB50B9C4B39eejj99@194.109.6.166> <28c08bf8-fb92-4487-acd1-10f9ea445d16@googlegroups.com> <bd5236f5-a271-4e96-89a9-590d490b49f8@googlegroups.com>

Show all headers | View raw


Ram Tobolski wrote:

>> Then, there would variable length of such elements, how do I replace all
>> the elements with class name of "Space" with another class?
> 
> Like this?
> 
> let elements = document.getElementsByClassName("Space");
> elements.forEach(element => element.className = newClass);

This cannot work because d.gEBCN() only returns a reference to an array-
*like* object which does not have a forEach() method by default.  You have 
to convert that object to an Array instance, or call 
Array.prototype.forEach() explicitly:

  [].forEach.call(elements, element => element.className = newClass);

Besides, the question was gibberish.  Your code does not replace the 
elements, it replaces the *class attribute value* of matching elements.

It remains to be seen whether that is really what the OP wants.  It is 
equally possible that they only want to exchange the class name in the 
attribute value:

  let oldClassName = "Space";
  [].forEach.call(
    document.getElementsByClassName(oldClassName),
    element => {
      element.classList.remove(oldClassName);
      element.classList.add(anotherClassName);
    });

      [We can reasonably assume that HTML user agents that provide an 
       ECMAScript 2015+ implementation for “let” and the arrow function
       syntax also support the DOM4 “classList” property.  The vice-versa
       assumption is not valid.]

Usually you would just add or remove a class name to/from the attribute 
value instead.  A stylesheet that requires renaming of a class name in
the attribute value for proper presentation is most certainly ill-designed.

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

How to replace a class? justaguy <lichunshen84@gmail.com> - 2016-11-08 18:53 -0800
  Re: How to replace a class? justaguy <lichunshen84@gmail.com> - 2016-11-10 07:45 -0800
    Re: How to replace a class? Ram Tobolski <ramtob@gmail.com> - 2016-11-10 10:08 -0800
      Re: How to replace a class? justaguy <lichunshen84@gmail.com> - 2016-11-10 11:58 -0800
        Re: How to replace a class? justaguy <lichunshen84@gmail.com> - 2016-11-10 13:06 -0800
          Re: How to replace a class? Ram Tobolski <ramtob@gmail.com> - 2016-11-10 13:37 -0800
            Re: How to replace a class? justaguy <lichunshen84@gmail.com> - 2016-11-10 14:09 -0800
              Re: How to replace a class? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-11-11 00:01 +0100
          Re: How to replace a class? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-11-10 22:51 +0100
            Re: How to replace a class? justaguy <lichunshen84@gmail.com> - 2016-11-10 14:09 -0800
        Re: How to replace a class? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-11-10 23:20 +0100
          Re: How to replace a class? John Harris <niam@jghnorth.org.uk.invalid> - 2016-11-11 10:24 +0000
            Re: How to replace a class? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-11-11 12:37 +0100
              Re: How to replace a class? John Harris <niam@jghnorth.org.uk.invalid> - 2016-11-12 17:09 +0000
          Re: How to replace a class? John Harris <niam@jghnorth.org.uk.invalid> - 2016-11-11 10:41 +0000
            Re: How to replace a class? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-11-11 12:43 +0100
              Re: How to replace a class? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-11-11 14:52 +0100
                Re: How to replace a class? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-11-11 15:11 +0100
                Re: How to replace a class? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-11-11 15:30 +0100
                Re: How to replace a class? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-11-11 16:41 +0100
                Re: How to replace a class? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-11-11 23:54 +0100
              Re: How to replace a class? John Harris <niam@jghnorth.org.uk.invalid> - 2016-11-12 18:42 +0000
            Re: How to replace a class? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-11-11 14:46 +0100
              Re: How to replace a class? John Harris <niam@jghnorth.org.uk.invalid> - 2016-11-12 14:51 +0000
                Re: How to replace a class? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-11-12 20:13 +0100
                Re: How to replace a class? justaguy <lichunshen84@gmail.com> - 2016-11-12 11:35 -0800
                Re: How to replace a class? John Harris <niam@jghnorth.org.uk.invalid> - 2016-11-13 11:13 +0000
      Re: How to replace a class? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-11-10 23:11 +0100
        Re: How to replace a class? justaguy <lichunshen84@gmail.com> - 2016-11-10 15:08 -0800
    Re: How to replace a class? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-11-10 20:37 +0100
      Re: How to replace a class? justaguy <lichunshen84@gmail.com> - 2016-11-10 11:59 -0800
        Re: How to replace a class? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-11-10 21:01 +0100

csiph-web