Cheerio: Ability to retrieve tagName

Created on 8 Apr 2013  Â·  6Comments  Â·  Source: cheeriojs/cheerio

Ideally, in jQuery's style: $ele.prop('tagName')

Most helpful comment

You could try doing something like...

var tagName = elem[0].tagName || elem[0].name;

Should work in browser & server.

All 6 comments

You can do $ele[0].name

That's what I am doing right now. But, I am working on a project where the code is executed both on the server side and in the browser, and doing $ele[0].name does not work in the browser. An alternative approach would be the availability of a .get() method, so I can do $ele.get(0).name on both the client and server side.

Right now I am using feature detection to deal with both environments.

I think you could just do….

Browser:

$ele[0].tagName

Cheerio:

$ele[0].name

On Apr 7, 2013, at 10:38 PM, Kishore Nallan [email protected] wrote:

That's what I am doing right now. But, I am working on a project where the code is executed both on the server side and in the browser, and doing $ele[0].name does not work in the browser. An alternative approach would be the availability of a .get() method, so I can do $ele.get(0).name on both the client and server side.

Right now I am using feature detection to deal with both environments.

—
Reply to this email directly or view it on GitHub.

Thanks - yes, that indeed works. This issue is more about maintaining consistency with jQuery's API.

tagName is DOM-specific. Cheerio's internal structure has nothing to do with the DOM. If you're looking for something that emulates the DOM, I would take a look at jQuery + JSDOM.

On Apr 7, 2013, at 10:43 PM, Kishore Nallan [email protected] wrote:

Thanks - yes, that indeed works. This issue is more about maintaining consistency with jQuery's API.

—
Reply to this email directly or view it on GitHub.

You could try doing something like...

var tagName = elem[0].tagName || elem[0].name;

Should work in browser & server.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

clayrisser picture clayrisser  Â·  4Comments

AlbertoElias picture AlbertoElias  Â·  4Comments

misner picture misner  Â·  3Comments

M3kH picture M3kH  Â·  4Comments

Canop picture Canop  Â·  3Comments