0

I was trying to follow the answer from here: Create XML in Javascript

I am getting an error in the console "undefined is not a function" for the 2nd line here:

var mat = document.createElement("mat");
imgsource = mat.createAttribute("imgsrc");
imgsource.nodeValue = default_matte_source;

total_size = mat.createAttribute("total_size");
total_size.nodeValue = 7.5;

cpu = mat.createAttribute("cpu");
cpu.nodeValue = 12;

cid = mat.createAttribute("cid");
cid.nodeValue = default_matte_cid;
Community
  • 1
  • 1
AllisonC
  • 2,973
  • 4
  • 29
  • 46

1 Answers1

2

createAttribute() is a method of document, not of individual nodes. You'll want something like:

imgsource = document.createAttribute('imgsrc');
imgsource.nodeValue = default_matte_source;
mat.setAttributeNode(imgsource);
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
  • How is this different from doing `setAttribute('imgsrc',default_matt_source)`? or both produce same results? – sabithpocker May 27 '14 at 15:11
  • 1
    Essentially the same result; this one (a) returns the attribute element if you wish to manipulate it further and (b) doesn't lower-case the attribute name when working in an HTML context (`setAttribute()` does). – Paul Roub May 27 '14 at 15:15