Parsing unknown element attributes in an XML file using jquery

Is it possible to get both attribute name and element value?

Basically, I want to get the name and values ​​of all the attributes of one element in order to write them into a Javascript object as follows:

obj {
  key: "value",
  another_key: "another_value",
}
+3
source share
1 answer

Using jQuery, you can get the name and values ​​of all attributes for a specific element:

var obj = {}; // this object will hold key-value pairs of elem attributes
var attribs = $('#elem')[0].attributes;
for (var i = 0; i < attribs.length; i++) {
    obj[attribs[i].name] = attribs[i].value;
}

Remember to replace '#elem'with a valid selector for the element from which you want to get the attributes.

+4
source

Source: https://habr.com/ru/post/1712618/


All Articles