BernardoLima
BernardoLima

Reputation: 1141

How to select all inputs by name?

I know you can use this to get all elements with the name 'target':

document.getElementsByName('target')

But what if I want to select only inputs with that name?

Upvotes: 1

Views: 123

Answers (3)

loveNoHate
loveNoHate

Reputation: 1547

Since you can catch forms by name you can do

<form name="outerTarget">    
    <input type="button" value="test" name="target">
</form>
<script>
document.forms['outerTarget'].target;
</script>

MDN: https://developer.mozilla.org/en-US/docs/Web/API/Element.name#Example.

Upvotes: 1

suff trek
suff trek

Reputation: 39777

In pure JavaScript you can use document.querySelectorAll

document.querySelectorAll('input[name="target"]');

Upvotes: 5

tymeJV
tymeJV

Reputation: 104775

You can do:

var inputs = document.querySelectorAll("input[name=target]");

Upvotes: 2

Related Questions