1

if I have multiples html input tags and I want to access those with "radio" type how can i do that using the type?(not by id or name or class). I tried somethings like: document.getElementsByTagName("input[type='radio']")[0];

1 Answer 1

1

If you want to use a selector string, you'll have to use querySelectorAll, not getElementsByTagName.

If you want to select all elements that match the selector string, you also shouldn't look up the array index immediately - instead, store the whole collection.

const inputs = document.querySelectoAll("input[type='radio']");

// do stuff with inputs[0], inputs[1], etc

In similar situations, if you have a selector string and you only want to use the first element that matches it, use querySelector instead of querySelectorAll:

const input = document.querySelector('input.someClass');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.