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];
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');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.