---
title: Parsing noscript Elements using Cheerio in Node.js
description: noscript elements are hard to handle when parsing HTML. Learn how to parse them with Cheerio in Node.js and build a mental model of how they work.
url: https://rishi.app/blog/parsing-noscript-elements-using-cheerio-in-node-js
author: Rishi Raj Jain
published_at: 2023-05-26T07:16:18.138Z
updated_at: 2026-10-14T22:26:37.602Z
---

# Parsing noscript Elements using Cheerio in Node.js

`<noscript>` elements are not easy to handle while parsing HTML. In this guide, I talk about how to parse noscript elements with via [Cheerio](https://npmjs.com/package/cheerio), while establishing a mental model of they work.

## Requirements

Before we begin, make sure you have Node.js and Cheerio installed in your project. You can install Cheerio by running the following command in your project directory:

```bash
# NPM
npm i -D cheerio

# Yarn
yarn add -D cheerio
```

## Getting Started

### Mental Model

- Treat `noscript` as a HTML in itself, hence gets a different cheerio selector for it
- You'd have to manage all noscript cheerio selectors differently than the page's cheerio selector
- You can switch between the two and use the .html() to replace/add HTML to any of the selector


### Code

Now, let's start with the steps to parse <noscript> elements with Cheerio:

#### 1. Import the necessary dependencies:

```js
// File: index.js

const { load } = require('cheerio')
```
    
#### 2. Load the HTML content that contains the <noscript> elements:

```js
// File: index.js

const html = `<html>
    <body>
        <noscript>
            This is a noscript element.
            <img id="noscript-image-1" src="/assets/img-1.png" />
            <img id="noscript-image-2" src="/assets/img-2.png" />
        </noscript>
    </body>
</html>`

const $ = load(html)    
```
    
#### 3. Treat noscript element as an independent HTML in itself:

```js
// File: index.js

const noscriptElements = $('noscript')
    
noscriptElements.each((index, element) => {
  const $2 = load($(element).html())
})
```

#### 4. Process the cheerio parent relative to the noscript element in the loop via $2:


```js
// File: index.js

const imgElements = $2('img')

imgElements.each((index, imgElement) => {
    const src = $2(imgElement).attr('src')
    const id = $2(imgElement).attr('id')
    console.log('Image Source:', src)
    console.log('Image ID:', id)
})
```
    
#### 5. Full Code
    
```js
// File: index.js

const { load } = require("cheerio")

const html = `<html>
    <body>
        <noscript>
          This is a noscript element.
          <img id="noscript-image-1" src="/assets/img-1.png" />
          <img id="noscript-image-2" src="/assets/img-2.png" />
        </noscript>
    </body>
</html>`

const $ = load(html)

const noscriptElements = $('noscript')

noscriptElements.each((index, element) => {
  const $2 = load($(element).html())
  const imgElements = $2('img')
  imgElements.each((index, imgElement) => {
    const src = $2(imgElement).attr('src')
    const id = $2(imgElement).attr('id')
    console.log('Image Source:', src)
    console.log('Image ID:', id)
  })
})
```

## Summary

In te code, we first locate all <noscript> elements using the $('noscript') selector. Then, we iterate over each <noscript> element and treat them as their own HTML and parse it again with cheerio. With these two looking alike selectors (here $ and $2) it becomes easier to switch between noscript HTML and the original HTML containing the noscript elements.

The current steps above demonstrate how to locate and access <img> elements inside <noscript> tags using Cheerio.

You can adjust the code to handle multiple <noscript> elements or perform additional processing based on your requirements.

---

This is the Markdown version of https://rishi.app/blog/parsing-noscript-elements-using-cheerio-in-node-js. Every page on this site is listed with a summary at https://rishi.app/sitemap.md.
