What is the most efficient way to select all dom elements that have a certain attribute.
<input name="mode">
With plain javascript I would use : document.querySelectorAll("[name='mode']")
or document.querySelectorAll("[name]")
if I don't care about the attribute value.
The [attribute|="value"] selector is used to select elements with the specified attribute, whose value can be exactly the specified value, or the specified value followed by a hyphen (-). Note: The value has to be a whole word, either alone, like class="top", or followed by a hyphen( - ), like class="top-text".
Cheerio get element attributesAttributes can be retrieved with attr function. import fetch from 'node-fetch'; import { load } from 'cheerio'; const url = 'http://webcode.me'; const response = await fetch(url); const body = await response. text(); let $ = load(body); let lnEl = $('link'); let attrs = lnEl.
Cheerio parses markup and provides an API for traversing/manipulating the resulting data structure. It does not interpret the result as a web browser does. Specifically, it does not produce a visual rendering, apply CSS, load external resources, or execute JavaScript which is common for a SPA (single page application).
Ok I found it in the cheerio documentation, here is how you do it:
$('[name=mode]')
cheerio docs: Selectors
For some reason, the accepted answer didn't work for me (using cheerio ^1.0.0-rc.2 here).
But for the following markup:
<input value="123" name="data[text_amount]">
this did work:
$('input[name="data[text_amount]"]'));
The double quote did the magic. Got that from cheerio's help docs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With