Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Aug 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Adding inputs into an array.

    I want to make a script that will insert all text type inputs into an array. From there I want to be able to call them and edit them. Here is what I have so far and it is not working.

    var phone1 = '702'
    var inputArray = new Array();
    var inputs = document.getElementsByTagName('input');
    inputs;
    if (input.type == 'text') { inputArray.push(inputs.id); }
    inputArray.reverse();

    inputArray[0].value = phone1;

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,195
    Thanks
    75
    Thanked 4,342 Times in 4,308 Posts
    That code makes no sense.

    Code:
    var phone1 = '702'
    var inputArray = [];
    var inputs = document.getElementsByTagName('input');
    for ( var i = 0; i < inputs.length; ++i )
    {
        var input = inputs[i];
        if (input.type == 'text') { inputArray.push(input); }
    }
    // not sure why you are doing this:
    inputArray.reverse();
    inputArray[0].value = phone1;
    You could do it more easily by giving all the relevant <input> fields the same name, if that would make sense in your other code.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New to the CF scene
    Join Date
    Aug 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    the reverse is because the push adds them in incorrect order.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,195
    Thanks
    75
    Thanked 4,342 Times in 4,308 Posts
    So run the loop in reverse order:
    Code:
    var inputArray = [];
    var inputs = document.getElementsByTagName('input');
    for ( var i = inputs.length - 1; i >= 0; --i )
    {
        var input = inputs[i];
        if (input.type == 'text') { inputArray.push(input); }
    }
    inputArray[0].value = phone1;
    You know, if you really only want to find the last <input> and don't need all the other elements, you could just do this:
    Code:
    var inputs = document.getElementsByTagName('input');
    for ( var i = inputs.length - 1; i >= 0; --i )
    {
        if ( inputs[i].type == "text" )
        {
            inputs[i].value = phone1;
            break;
        }
    }
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •