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.

Undocumented attribs property safe to use?

See original GitHub issue

I was looking for a way to move all of one element’s attributes to another element and noticed the attribs property on cheerio elements which seemed to be exactly what I needed but it’s not documented anywhere that I could see. It is however used in the example in htmlparser2’s readme so I’m wondering if this property is safe to use of if there’s a better way getting at all attributes.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
JamesMGreenecommented, Jan 11, 2021

To clarify on that unfortunately cryptic closure message, it would appear the TypeScript type definitions show an attr() method that can be invoked without any arguments to return a simple object map of key-value pairs:

https://github.com/cheeriojs/cheerio/blob/a3363012de3ddf4dafe91e89864d8feca9d28722/types/index.d.ts#L18

As shown in tests also:

https://github.com/cheeriojs/cheerio/blob/a3363012de3ddf4dafe91e89864d8feca9d28722/test/api/attributes.js#L16-L20

0reactions
plocketcommented, Mar 15, 2021

I’m finding that currently .attr('value') and .attribs.value are returning two different things. Not sure if this is the right place to put this, but I found this thread while trying to find some context for this situation. I’m hoping this will help someone else with the same confusion in the future.

const cheerio = require('cheerio');
const html = '<input class="my_inputs" type="checkbox">';
const $ = cheerio.load( html );

let node = $( '.my_inputs' )[0];
let $node = $( node );
console.log( node.attribs.value );  // undefined (this is the result I would want)
console.log( $node.attr( 'value' ));  // 'on'

.attr('value') returns ‘on’ while .attribs.value returns undefined. If you’re looking for the ‘real’ value attribute, .attribs.value seems to give the most accurate to me at the moment for what it’s worth.

Again, not necessarily reporting a bug, just hoping to help some future person figure out how to get the [literal] HTML attribute value with cheerio.

[Edit: To be clear, I’m not talking about using $node.val(). .va() does a lot of fancy logic to get what a human might think of as the ‘value’ of the node. See #451 talking about that a bit more. Here, I’m talking about the literal value of the value attribute.]

Read more comments on GitHub >

github_iconTop Results From Across the Web

Undocumented classdef attributes
If true , then such properties can be accessed using any case. For example, if the property is called myProp , then it...
Read more >
Landlords and Undocumented Tenants: An Overview
By applying the same requisites to each applicant, a landlord can avoid renting to anyone – not just undocumented persons – who could...
Read more >
c# - Why is the attribute target 'typevar' undocumented?
Actually, typevar attribute target is documented, but it seems that only in the standardized C# 2.0 Language Specification: ...
Read more >
California And Illegal Immigrant Tenant Rights - ADHI Schools
Illegal immigrants live somewhere and with California's notoriously high prices, renting is the only option for many. This raises questions for the landlord....
Read more >
5 Rights of Undocumented Immigrants | Clearwater Law Group
The Fifth Amendment, for example, states that “No person shall be deprived of life, liberty, or property, without due process of law.” And...
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