Returns an array of all inputs with specified name I've made a nice little function to return a NodeList array containing all the inputs that have a specified name attribute:



function GiveElementsByName(name) {
var finalOutput = [], times = document.getElementsByTagName('input').length, inOutput = 0, inAll = 0; //For use later.
while(times--)
{
if (document.getElementsByTagName('input')[inAll].name == name) //Apply to all inputs with specified name.
 {
 finalOutput[inOutput] = document.getElementsByTagName('input')[inAll]; //Array with all the matching inputs.
 inOutput++;
 }
inAll++;
}
return finalOutput;
}
function MyFunc() {
  GiveElementsByName("Lol1")[1].value = "I don't like potatos anymore";
}
&lt;input type="text" name="Lol1" value="I like potatos"&gt;
&lt;input type="text" name="Lol1" value="I like potatos"&gt;
&lt;input type="text" name="Lol1" value="I like potatos"&gt;
&lt;input type="text" name="Lol1" value="I like potatos"&gt;
&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;
&lt;input type="text" name="Lol2" value="I like potatos more"&gt;
&lt;input type="text" name="Lol2" value="I like potatos more"&gt;
&lt;input type="text" name="Lol2" value="I like potatos more"&gt;
&lt;input type="text" name="Lol2" value="I like potatos more"&gt;
&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;
&lt;input type="button" value="Click me!" onClick="MyFunc()"&gt;




Could you help me shrink down the function and make it quicker?
The GiveElementsByName function is poorly indented.
And the variable declarations should not be only a single extremely long var line,
but each variable should be on its own line.
It's better like this:

function GiveElementsByName(name) {
    var finalOutput = [],
        times = document.getElementsByTagName('input').length,
        inOutput = 0,
        inAll = 0;
    while (times--) {
        if (document.getElementsByTagName('input')[inAll].name == name) {
            finalOutput[inOutput] = document.getElementsByTagName('input')[inAll]; 
            inOutput++;
        }
        inAll++;
    }
    return finalOutput;
}


It seems to me that there is an easier way to do this using document.querySelectorAll, which should be supported by modern browsers:

function GiveElementsByName(name) {
    return document.querySelectorAll('input[name=' + name + ']');
}