element.setAttribute is not a function

5

or this:

var link = "http://www.someurl.com";
var preview = document.getElementById("preview"); //getElementById instead of querySelectorAll
preview.setAttribute("data", link);

Be sure to run the code after the element is created, or use jQuery code:

$( document ).ready(function() {
}

“Uncaught TypeError: Cannot read property ‘setAttribute’ of null”

By: LazarusRising—in that case, the element doesn’t exist yet in the document. You need to run the code after the element is created, say after the load event or a script below the element.

Leave a Comment