To get an element by type in JavaScript, you can use the document.querySelector()
method along with a CSS selector that specifies the type of element you want to select.
For example, if you wanted to select the first <input>
element on the page, you could do:
index.tsx47 chars2 lines
If you wanted to select all <p>
elements on the page, you could do:
index.tsx51 chars2 lines
You can also select elements by other attributes, such as their class names or IDs:
index.tsx110 chars3 lines
Keep in mind that querySelector()
and querySelectorAll()
are part of the DOM API, so they only work on elements that are part of the document tree. You can't use them to select elements that aren't part of the current page.
gistlibby LogSnag