Javascript – Remove element created by JavaScript on load

javascript

How can I remove elements which are created by JavaScript, I tried using CSS by setting display:none; but that doesn't seem to solve my problem, also I can't remove them since I don't have them in HTML, any other ways? Thank you

UPDATE:

Can't use any JavaScript such as jQuery, MooTools, ExtJS etc, and actual elements I want to remove are divs, with a specified class so I can't use getElementById.

I found this script on Google, but it doesn't seem to work but this is what I need:

HERE

Best Answer

You will want something like this to take advantage of browser support where you can:

if(document.getElementsByClassName){
    // Safari 3+ and Firefox 3+
    var itms = document.getElementsByClassName('your_class');
    for(var i = 0; i<itms.length; i++){
        var it = itms[i];
        if(it.tagName == "DIV"){
            it.parentNode.removeChild(it);
            i = i - 1; //It actually gets removed from the array, so we need to drop our count
        }

    }
} else {
    // All other browsers
    var itms = document.getElementsByTagName('div');
    for(var i = 0; i<itms.length; i++){
        var it = itms[i];
                              // Test that className contains your class
        if(/your_class/.test(it.className)) it.parentNode.removeChild(it);
    }
}