question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Retrieving all attributes of an Element

See original GitHub issue

I recently needed to retrieve all attributes of an Element to loop through the elements. After awhile of beating my head against the tutorials on doing this with jQuery (e.g. use the .attributes object) I remembered that Cheerio doesn’t implement everything …

I did find inside one of the source files that the $(element) object has a .attribs array and was able to write my code to access that array. But it’s not compatible with the existing suggestions out there for using jQuery API approaches.

For example http://stackoverflow.com/questions/2048720/get-all-attributes-from-a-html-element-with-javascript-jquery

var element = $("span[name='test']");
$(element[0].attributes).each(function() {
console.log(this.nodeName+':'+this.nodeValue);});

What I did instead is:

var elem = $(element).get(0);
// attribs is an implementation detail of Cheerio
for (var nm in elem.attribs) {
    var attr = elem.attribs[nm];
    ....
}

Seems to me that at the time you create the .attribs array, you could also/instead create a compatible .attributes object.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:6
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

19reactions
MonsieurVcommented, Apr 29, 2020

Here is a wrapper that can be used between jQuery and Cheerio code:

const getAllAttributes = function (node) {
	return node.attributes || Object.keys(node.attribs).map(
	    name => ({ name, value: node.attribs[name] })
	);
};

Then instead of using directly element.attributes, you do getAllAttributes(element). The API of the returned attributes is the same as for element.attributes.

Example (usage with cheerio):

const cheerio = require('cheerio');

const $ = cheerio.load('<ul id="tomato" class="vegetables" disabled></ul>');
console.log(getAllAttributes($('ul').get(0)));

// Output:
// [
//   { name: 'id', value: 'tomato' },
//   { name: 'class', value: 'vegetables' },
//  { name: 'disabled', value: '' }
// ]

Edit: after @bogem comment, updated with a Cheerio working example, and an implementation without Lodash/_.

3reactions
fb55commented, Dec 4, 2015

Supporting the entire DOM API is what jsdom is for, cheerio (probably) won’t implement the attributes interface. You are in luck, though: attribs is already an object, with the attribute name as the key and the value as the entry for the key.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Get all Attributes from a HTML element with Javascript/jQuery
It retrieves the names of all the element's current attributes as a regular Array, that you can then reduce to a nice object...
Read more >
Element.attributes - Web APIs - MDN Web Docs - Mozilla
The Element.attributes property returns a live collection of all attribute nodes registered to the specified node.
Read more >
How to get all the attributes of an element using JavaScript?
To get all the attributes of a DOM element, we can use the attributes property on the element using JavaScript.
Read more >
getting all attributes and values of an element - jQuery Forum
I would like to be able to get all of the attributes and their values with an output similar to: [0] class: {...
Read more >
How to get the data attributes of an element using JavaScript
Using the dataset property; Using .getAttribute() method. We will discuss both the ways to get the data attribute of an element. Approach:.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found