0

I need change style's in <div> from {align, left} to {align, right}

function changeAttribute() {
    var gg = document.getElementsByTagName("div");
    var allP = gg[12].childNodes;
    var par, align;
    for (i = 0; i < allP.length; i++) {
        par = allP[i];
        gAl = par.getAttribute("align");
        if (!gAl) {
            continue
        } else if (gAl == "right") {
            align = par.setAttribute("align", "left");
        }
        align = par.setAttribute("align", "right");
    }
}
Sibeesh Venu
  • 18,755
  • 12
  • 103
  • 140
Leo
  • 1

1 Answers1

0

Seems like your missing an else statement in the end.
align always gets set "right".

var gg  = document.getElementsByTagName("div");
var allP = gg[12].childNodes;

var par, align;
for (i = 0; i < allP.length; i++){
    par = allP[i];
    if (gAl.nodeType !== Node.ELEMENT_NODE) continue;
    gAl = par.getAttribute("align");
    if(!gAl){
        continue
    } else if ( gAl == "right"){
        align = par.setAttribute("align", "left");
    } else {
        align = par.setAttribute("align", "right");
    }
}
Armin Bu
  • 1,330
  • 9
  • 17
  • it does not answer the question. and it still has the same problem, par.getAttribute call will fail. – Vladimir M Feb 16 '18 at 10:04
  • @VladimirM Why would a

    tag not have the setAttribute() method defined, since all [DOM Elements](https://developer.mozilla.org/de/docs/Web/API/Element/setAttribute) have it. [Additionally](https://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-F68F082)

    – Armin Bu Feb 16 '18 at 12:22
  • @Reijo the thing is that you are not getting a list of elements, but a list of nodes. Elements are subset of nodes. you can see more on that difference here: https://stackoverflow.com/questions/9979172/difference-between-node-object-and-element-object – Vladimir M Feb 16 '18 at 12:45
  • Thanks for pointing this out! I'll edit the code, to handle this issue. That post also says that a nodeList is an array-like list of nodes. Of which each node is an Element if all children of gg[12] are of type Element. (see last paragraph before the edit) – Armin Bu Feb 16 '18 at 12:58