Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-13-2010, 10:39 PM   PM User | #1
Stax
New to the CF scene

 
Join Date: Aug 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Stax is an unknown quantity at this point
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;
Stax is offline   Reply With Quote
Old 08-13-2010, 10:43 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,579
Thanks: 62
Thanked 4,064 Times in 4,033 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 08-13-2010, 10:45 PM   PM User | #3
Stax
New to the CF scene

 
Join Date: Aug 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Stax is an unknown quantity at this point
the reverse is because the push adds them in incorrect order.
Stax is offline   Reply With Quote
Old 08-14-2010, 12:42 AM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,579
Thanks: 62
Thanked 4,064 Times in 4,033 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:39 AM.


Advertisement
Log in to turn off these ads.