Search code examples
javascripthtmlinternet-explorerdomchaining

Why does chaining not work in IE8 / 9


Why does this work:

el = document.getElementById('STR');
if( el ){
    el.checked = true;
    el2 = el.cloneNode(false);
    el.parentNode.insertBefore(el2, el);
    el2.setAttribute('id','');
    el2.setAttribute('disabled','disabled');
    el2.removeAttribute('name');
    el.removeAttribute("disabled");
    el.style.display="none";
}

but this not:

el = document.getElementById('STR');
if( el ){
    el.checked = true;
    el2 = el.cloneNode(false);
    el.parentNode.insertBefore(el2, el);
    el2.setAttribute('id','');
    el2.setAttribute('disabled','disabled').removeAttribute('name');
    el.removeAttribute("disabled");
    el.style.display="none";
}

In Firefox / Chrome the last snippet works perfectly.


Solution

  • That does not actually work in any browser I know of. The DOM "setAttribute()" function returns undefined in Firefox and Chrome (and, probably IE as well).

    The "chaining" code style is something that you can do in JavaScript if you want, but it requires a framework of some sort to explicitly implement it. DOM methods generally do not work that way.