inputs is a jQuery collection. You will get the first element of that collection every time you only access inputs But if you loop over inputs with each, then $(this).attr("name) or this.name will return the attribute of each element
PS: .each IS a for loop - here is some of the code from the jQuery.js file
if ( isArrayLike( obj ) ) {
length = obj.length;
for ( ; i < length; i++ ) {
if ( callback.call( obj[ i ], i, obj[ i ] ) === false ) {
break;
}
}
}
You have several possibilities
$(this).attr("name") or this.name
var $inputs = $("input[type='text']"),
outputs = [];
$("#submit").click(function() {
$inputs.each(function() {
outputs.push($(this).attr("name")); // $(this) is the input; this.name works too
});
console.log(outputs)
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
<label for="Forename">
Forename
</label>
<input type="text" id="Forename" name="forename">
</div>
<div>
<label for="Middlename">
Middle Names
</label>
<input type="text" id="Middlename" name="middlename">
</div>
<div>
<label for="Lastname">
Lastname
</label>
<input type="text" id="Lastname" name="lastname">
</div>
<p id="message"></p>
<input type="button" id="submit" value="submit">
accessing the input's DOM node using the parameters passed to the function
var inputs = $("input[type='text']"),
outputs = [];
$("#submit").click(function() {
inputs.each(function(_,inp) { // inp is the DOM node
outputs.push(inp.name);
});
console.log(outputs)
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
<label for="Forename">
Forename
</label>
<input type="text" id="Forename" name="forename">
</div>
<div>
<label for="Middlename">
Middle Names
</label>
<input type="text" id="Middlename" name="middlename">
</div>
<div>
<label for="Lastname">
Lastname
</label>
<input type="text" id="Lastname" name="lastname">
</div>
<p id="message"></p>
<input type="button" id="submit" value="submit">
Use a map:
let outputs;
$("#submit").click(function() {
outputs = $("input[type='text']").map((_,inp) => inp.name).get()
console.log(outputs)
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
<label for="Forename">
Forename
</label>
<input type="text" id="Forename" name="forename">
</div>
<div>
<label for="Middlename">
Middle Names
</label>
<input type="text" id="Middlename" name="middlename">
</div>
<div>
<label for="Lastname">
Lastname
</label>
<input type="text" id="Lastname" name="lastname">
</div>
<p id="message"></p>
<input type="button" id="submit" value="submit">
outputsis still empty after clicking the button" No, it isn't. Your snippet doesn't ever useoutputsafter clicking the button. I copied it, addedconsole.log(outputs)to just after theeachcall in theclickhandler, andoutputshas the names in it. Edit: mplungjan changed your snippet to outputoutputs. Now it has the first name repeated in it, because you're doinginputs.attr("name"), notthis.name. Usethis.nameto access the name for the input theeachloop is visiting.this.namedoes work.