Tuesday, June 4, 2024
 Popular · Latest · Hot · Upcoming
13
rated 0 times [  14] [ 1]  / answers: 1 / hits: 87305  / 8 Years ago, thu, february 25, 2016, 12:00:00

So, i know that this has already been answered, but none of the previous answers managed to make my code work. I have a html structure as the following:



<div class=form>
<div class=formrow>
<div class=previewcontainer>
<object id=preview>
<object>
</div>
</div>
</div>


I am trying to set the data attribute to the object like this:



var link = http://www.someurl.com;
var preview = document.querySelectorAll (#preview);
preview.setAttribute(data, link);


However, I get an error preview.setAttribute is not a function


More From » object

 Answers
10

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() {
}




@Lazarus Rising mentioned,



Uncaught TypeError: Cannot read property 'setAttribute' of null



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.


[#63165] Tuesday, February 23, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ervindouglasm

Total Points: 451
Total Questions: 103
Total Answers: 102

Location: Turkmenistan
Member since Thu, Dec 1, 2022
2 Years ago
ervindouglasm questions
Tue, Jul 20, 21, 00:00, 3 Years ago
Wed, Jan 6, 21, 00:00, 3 Years ago
Fri, Nov 13, 20, 00:00, 4 Years ago
Tue, Oct 6, 20, 00:00, 4 Years ago
Tue, Aug 25, 20, 00:00, 4 Years ago
;