Attempting to convert some JavaScript
code into TypeScript
and create an HTML element from the ts
. Here is the function causing issues:
createSvgElem(name: string, attributes: any) {
let node = document.createAttributeNS('http://www.w3.org/2000/svg', name);
for (let name in attributes) {
node.setAttribute()
}
}
An error is thrown:
Property 'setAttribute' does not exist on type 'Attr'.
The original format (working perfectly within a js file):
HocrProofreader.prototype.createSvgElem = function (name, attributes) {
var node = document.createElementNS('http://www.w3.org/2000/svg', name);
for (var name in attributes) {
node.setAttribute(name, attributes[name]);
}
return node;
},
What is the correct way to handle this?
[EDIT]
After changing to createElementNS
, it works fine. However, I am unable to access the linkedNode
property
const rectNode = this.createSvgElem('rect', {
'x': options.bbox[0],
'y': options.bbox[1],
'width': options.bbox[2] - options.bbox[0],
'height': options.bbox[3] - options.bbox[1],
'id': node.id,
'bbox': node.title,
'class': className
});
parentRectsNode.appendChild(rectNode);
// cross-link both nodes:
rectNode.linkedNode = node;
node.linkedNode = rectNode;
}