how to get an element by type in javascript

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.tsx
const input = document.querySelector('input');
47 chars
2 lines

If you wanted to select all <p> elements on the page, you could do:

index.tsx
const paragraphs = document.querySelectorAll('p');
51 chars
2 lines

You can also select elements by other attributes, such as their class names or IDs:

index.tsx
const myElement = document.querySelector('.my-class');
const otherElement = document.querySelector('#my-id');
110 chars
3 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