Change the innerHTML of a tag

1

I have a small problem, when I change a property of any tag using Javascript the browser says:

  

Uncaught TypeError: Can not set property 'src' of null

But I tested it in JSFiddle and it worked correctly with the same parameters!

Code:

function get(idOf) {
    return document.getElementById(idOf);
}

function loadData(id, videoDATA) {
    get(id).src = "https://www.youtube.com/embed/" + videoDATA;
}

window.onload = function () {
    get('404').innerHtml = '<iframe src="" width="500px" height="400px" frameborder="0" id="frame0"></iframe>';
    loadData('frame0', 'kdWAmMRmELg');
}

<div id="404">...</div> is a DIV that I put in if videoDATA was not loaded.

    
asked by anonymous 21.01.2015 / 19:21

2 answers

2

This error occurs because you are trying to change the src of an element that was not found on the page. And it was not found because the line that tries to create it is wrong. You need to use innerHTML instead of innerHtml :

window.onload = function () {
    get('404').innerHTML = '<iframe src="" width="500px" height="400px" frameborder="0" id="frame0"></iframe>';
    loadData('frame0', 'kdWAmMRmELg');
}

link

    
09.02.2015 / 19:56
-1

Exchange window.onload by document.onready .

Probably in the JSFiddle of no error because the JavaScripts that are loaded are at the end of HTML where they sure are ready.

    
09.02.2015 / 19:33