How to get all elements which name starts with some string?

前端 未结 5 1323
日久生厌
日久生厌 2020-12-08 13:38

I have an HTML page. I would like to extract all elements whose name starts with \"q1_\".

How can I achieve this in JavaScript?

5条回答
  •  孤城傲影
    2020-12-08 14:22

    HTML DOM querySelectorAll() method seems apt here.

    W3School Link given here

    Syntax (As given in W3School)

    document.querySelectorAll(CSS selectors)
    

    So the answer.

    document.querySelectorAll("[name^=q1_]")
    

    Fiddle

    Edit:

    Considering FLX's suggestion adding link to MDN here

提交回复
热议问题