Cheerio does not retrieve disabled attribute

23 views Asked by At

I am using Node.js and Cheerio

Element from page

<button 
  type="submit" name="add" id="addToCart-product-template" 
  class="btn btn--wide btn--add-to-cart disabled" 
  disabled="">
  <span class="icon icon-cart"></span>
  <span id="addToCartText-product-template">Disabled</span>
</button>

Code

const $ = cheerio.load(html);
const element = $('[id=addToCart-product-template]').get(0);
console.log(element.attributes);

// Result 
// 0: {name: 'type', value: 'submit', namespace: undefined, prefix: undefined}
// 1: {name: 'name', value: 'add', namespace: undefined, prefix: undefined}
// 2: {name: 'id', value: 'addToCart-product-template', namespace: undefined, prefix: undefined}
// 3: {name: 'class', value: 'btn btn--wide btn--add-to-cart', namespace: undefined, prefix: undefined}
// length: 4

When I try with hard-coded data like this its work fine

const $ = cheerio.load(
            '<button 
                 type="submit" name="add" 
                 id="addToCart-product-template" 
                 class="btn btn--wide btn--add-to-cart disabled" disabled>
               <span class="icon icon-cart"></span>
               <span id="addToCartText-product-template">Disabled</span>
            </button>'
);
0

There are 0 answers