Luca Sarif Luca Sarif - 6 months ago 44
HTML Question

Make an array of input name attributes Javascript

How can I make an array using a loop which contains the name attributes of inputs inside a form tag, using javascript.

This is my code so far, but it doesn't seem to be working and I have looked at other similar questions to no avail.

<script type="text/javascript">
var inputnames[];
for (var i = 0; i < document.getElementsByTagName('input').length; i++)
{
inputnames[document.getElementsByTagName("input")[i].name]
};
document.write(inputnames)
</script>

Answer Source

The issue is that you're not actually adding the values to the array. Change

inputnames[document.getElementsByTagName("input")[i].name]

to

inputnames.push(document.getElementsByTagName("input")[i].name)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download