javascript createElement and setAttribute
Solution 1:
Your code will stop executing, throwing a title is not defined
exception at
video.setAttribute(title, "Test");
In setAttribute(title, ...)
you're using title
as though it was a variable named title, but that doesn't exist. You want instead to pass the string value "title"
:
video.setAttribute('title', 'Test');
... and the same for the other attributes...
A few more hints since you're using JSFiddle:
- Make a habit of saving the fiddle and always provide the link together with your question
- Under "Framework & Extensions" in the left-hand panel, make sure you've set the javascript to load with "no wrap", otherwise you won't be able to call the method from your html the way you've done. (This applies when you're defining your JavaScript functions in the JavaScript panel. I see from your updated question with included fiddle link that you're currently putting it all in the HTML markup, so you won't have this issue)
- Hit F12 in your browser, and you will be able to see the actual error message that your code produces.
Solution 2:
You need to wrap your attributes in quotes '
or double quotes "
:
video.setAttribute('title', "Test");
video.setAttribute('width', "440");
video.setAttribute('height', "390");
video.setAttribute('src', "http://www.youtube.com/embed/" + videoID);
video.setAttribute('frameborder', "0");
Fiddle Demo