Search code examples
javascripthtmliframejsfiddle

jsFiddle document.getElementById inside iFrame


I know jsFiddle displays it's results in an iFrame. I am taking a JS class right now and according to my instructor, "getElementById" is the most used function in all of JS.

So why doesn't "getElementById" work in jsFiddle:

document.getElementById("myH1")

However...

document.getElementsByTagName("H1")

Does work!

See my fiddle for the example:

http://jsfiddle.net/uM9t4/


Solution

  • it does, its just your thinking of how replace works is wrong, it does not do an inline change it returns the changed string, if it didnt work you would be getting an

    Cannot call method 'getAttribute' of null


    document.getElementById("myH1").getAttribute("class").replace("bye","NO");
    

    should be

    var change = document.getElementById("myH1").getAttribute("class").replace("bye","NO");
    document.getElementById("myH1").setAttribute("class",change);
    

    JSFiddle