Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to test value of multiple fields and update value if neccessary? 2

Status
Not open for further replies.

topjimmie

IS-IT--Management
Feb 4, 2002
28
US
I'm performing a very simple step as seen in the following code:

Code:
var d = new Date("Cancelled Date"); 
if (d.getFullYear< "1900") 
{ 
SetFieldValue( "Cancelled Date",  "");
}

On the same form, I have several date fields that must also be tested. Rather than write each one out, could someone show me an example of iterating through an array of fields and updating the values as needed? Thank you!
 
That depends a little on how your fields are set up. For instance if you have say 5 fields you need to check and change their value and all 5 have have a common name you could use getElementsByName to get an array of the fields to work with:

HTML:
<input type="text" name="textfield">
<input type="text" name="textfield">
<input type="text" name="textfield">
<input type="text" name="textfield">
<input type="text" name="textfield">


Code:
var fields=document.getElementsByName('textfield');

for(x in fields){
fields[x].value="new value";
}

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown. 

Behind the Web, Tips and Tricks for Web Development. 
[URL unfurl="true"]http://behindtheweb.blogspot.com/[/URL]
 
All of the fields are date type, and each has "date" in the field name. Is it possible to use wildcard for the field names in the array?
 
No, you can't use wildcards for the getElementsByName function.
You could possibly use the getElementsByTagName function to get all inputs, and then filter those by the name you wish to find.
For instance:

Code:
function getValues(){
[green]/*Get all form inputs in page */ [/green]
 var fields=document.getElementsByTagName('input');
[green] /* Set our array for the elements we want */[/green]
 var datefields =new Array();
[green]/* Set counter */[/green]
 var i=0;
[green]/* define pattern to look for */[/green]
 var patt1=/date/gi;
[green]/* recurse through all input elements */[/green]
 for(x in fields){
[green]/* if the current item has a name and it matches our patter add it to our array */[/green]
 if(fields[x].name && fields[x].name.match(patt1)){
    datefields[i]=fields[x];

  }
 }
}
the datefields array will then contain all inputs that have name which contains the string "date" in it.

Yes Jquery has implemented such a feature, and while it requires you to include an extra library in your pages, its small and offers quite a bit of flexibility. However as shown above it can be done through straight JS, but it takes a bit more coding. Essentially Jquery does the same thing but you just don't see it.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Now that's pretty cool! Thanks Vacunita, I'll give it go.

@whosrdaddy - Thank you also, looks like a great library, will keep in mind for additional projects
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top