1

I am trying to retrieve a specific section from an html page using javascript (NodeJS)

document.querySelectorAll("section#sectionID")

It works fine with section names starting with letters. But one section name starts with a number like for example

section#4_someText_here

and I tried fetching it in a number of ways. The way it is suggested is I think this one

querySelectorAll("section#\\4 _someText_here")

But it doesn't find the section. Is this the correct way to use it?

AndiAna
  • 854
  • 6
  • 26
  • Only letter, numbers, -, _ should be used, and an id should not start with a number. https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id ... you can escape them though https://codepen.io/estelle/pen/jOvzbgb – Ruskin May 10 '23 at 06:30
  • 1
    @Ruskin thanks for the codepen. very helpful. yes I know I wouldn't use a number to start section id either, but thats what the website I am parsing is using. below answer is good with addressing this specific problem though. – AndiAna May 10 '23 at 08:10
  • Related: [Can I have an element with an ID that starts with a number?](https://stackoverflow.com/questions/5672903/can-i-have-an-element-with-an-id-that-starts-with-a-number) – ggorlen May 10 '23 at 16:06

1 Answers1

2

You can use the attribute selectors with the id attribute:

document.querySelectorAll('section[id="4_someText_here"]');
Mamun
  • 66,969
  • 9
  • 47
  • 59