Should JavaScript add or remove data-nosnippet after an element exists?

The Citead team, updated

In short. Set data-nosnippet when JavaScript creates a new supported element and leave the attribute unchanged on existing nodes.

Definition: data-nosnippet marks text that Google should exclude from a search snippet. Google's guidance allows extraction before or after rendering, so an existing element should keep the attribute state that it had when it entered the DOM.

Key facts

  • Google's robots metadata reference says extraction of data-nosnippet can happen before and after rendering.
  • The same reference advises against adding or removing the attribute on existing nodes through JavaScript.
  • Google's JavaScript SEO basics describes crawling, rendering and indexing as separate processing phases, with rendered HTML used for indexing.
  • Google's URL Inspection documentation exposes page code and JavaScript output through the live inspection details.

What should JavaScript do when it creates an element?

For a new element, include data-nosnippet as the element is initially added to the page's DOM when its text needs exclusion. For an element that already exists, leave the attribute unchanged during later JavaScript updates. Google's metadata reference gives this rule because Google may extract the attribute from the response HTML or from a rendered page.

The attribute is supported on span, div, and section elements. It is a boolean attribute, so an assigned value does not change its meaning. The HTML must remain valid and the relevant tags must be closed.

const section = document.createElement("section");
section.setAttribute("data-nosnippet", "");
section.textContent = "Text excluded from the snippet";
document.body.append(section);

This pattern sets the attribute before the new element is inserted. A later script that toggles the attribute on that same section creates the timing ambiguity covered by Google's guidance.

Why does rendering affect the answer?

Google's JavaScript documentation says that parsing the HTML response works when the response contains all page content. With an app shell, the initial HTML can omit the actual content and JavaScript can generate it later. Googlebot queues pages with an HTTP 200 status for rendering, then a headless Chromium renders the page and executes JavaScript; Google also uses the rendered HTML to index the page.

That process explains why the initial response and the rendered DOM can expose different element states. The robots metadata reference therefore treats the extraction point as uncertain. It documents the stable choices: put the attribute in the original HTML for an element already present, or add it while JavaScript creates a new element.

How can you inspect the result?

In Search Console, run a live URL test and open View tested page. Google's URL Inspection documentation says this view can expose the rendered page's screenshot, raw HTML returned, HTTP headers, JavaScript console output and loaded resources. The same documentation also directs you to View tested page > More info for page code and other details.

Compare the element in the original response with its rendered representation. Check that a supported wrapper contains the intended text, that data-nosnippet is present when the element is first inserted, and that the HTML is correctly closed. Google also requires crawlers to access the page before these settings can be read and followed.

FAQ

Can JavaScript add data-nosnippet to an existing element?

Google's robots metadata reference advises against adding the attribute to an existing node through JavaScript because extraction may occur before or after rendering.

Can JavaScript remove data-nosnippet from an existing element?

The same Google guidance advises against removing the attribute from an existing node through JavaScript. Keep the intended state in the initial HTML or establish it when JavaScript creates the element.

What if a custom element needs data-nosnippet?

Google's specification says to wrap or render the custom element with a div, span, or section when data-nosnippet is needed.

Does this rule apply to every answer engine?

This entry describes Google's documented extraction and rendering behavior. The downloaded Google sources do not establish the behavior of other answer engines.

Sources

  1. Google Search Central, "Robots meta tag, data-nosnippet, and X-Robots-Tag specifications", 24 March 2026
  2. Google Search Central, "JavaScript SEO basics", 4 March 2026
  3. Google Search Console Help, "URL Inspection tool", undated

Check how a page reads to answer engines. Open the GEO score analyzer

Test a page and see what to fix first

The analysis usually takes less than 10 seconds and needs no account.

400,000+ pages scored in the public benchmark