Skip to content
Advertisement

How do you clone an element’s NamedNodeMap to an empty object?

I have a javaScript array of objects, where the objects are called text, where each object element contains information about an HTML element that looks like the following using the Chrome browser’s inspect Source Watch area:

JavaScript

Note: While the attributes member of text object (above) only contains the information for the label attribute, it could also contain other attributes items, such as a style and/or class, which were copied from a set of <li> tags in my web-page, in which case there would be additional entries in the text object and attributes member for those attributes items.

When the information <li> tags are collected, the tag’s attributes are copied into text object using the copyAttributes function that is shown below, which works fine.

However, when later try to use the same copyAttributes function to copy the text.attributes to the attr variable to then create a new HTML element, I get this error:

JavaScript

The following code shows how this call is made, and what I’m trying to with the information copied into attr object variable.

JavaScript

The problem seems to be that the to parameter of the copyAttributes function isn’t the NamedNodeMap type, so it doesn’t support the setNamedItem method.

How can I create an empty attributes variable of this type?

Or is there a better/easier way to populate the attributes variable with the test.attributes information?

Thanks.

Advertisement

Answer

Here is a codepen of a working version of the drow-down combobox that properly created the li tag elements and copies the desired attributes into it.

This create function is from the Awesomplete plug-in, but it tha been modified to not set the focud on the newly created element.

JavaScript

This is my modified code that calls the above create function and assign the attributes.

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement