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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Please explain the following code 1

Status
Not open for further replies.

scc

Programmer
Apr 30, 2001
218
US
I have this piece of code in an asp page that I'm trying to edit, and I'm struggling with understanding what this javascript is doing since I know very little about javascript.

Can someone please explain this line by line to help me get a better understanding.

function getInfo(theForm){
var location = "";
for (i=0;i<theForm.length;i++) {
var textbox = theForm.elements;
if (textbox.name == theForm.item.value) {
location = textbox.value;
}
}
if (location.substring(0,1)=="M"){
parent.location.href = location
}

This code is called by another javascript function which is in an onKeyPress event of a form so that when the user presses enter, it acts like it clicked a Continue button:

function manualSubmit(key, theForm){
if (event.keyCode==13) {
event.returnValue = false;
getInfo(theForm);
}
}

TIA!
 
The function is looping through a forms elements and i believe it's looking for a form element that has the same value for its 'name' and 'value' attributes, if this begins with an M then the top frame is redirected there.

I think (?) ;o)

HTH

Simon
 
For me to get a real grip on this, I need to understand line by line what the code is doing.

For instance, on the first line a variable by the name of location with a value of "" is created.

On the second line, I get lost since I don't know javascript..
 
function getInfo(theForm){

// create new var called location - set its value to ""
var location = "";

// loop through the element of the form assigned to var theForm
for (i=0;i<theForm.length;i++) {

// set var textbox as the form element object
var textbox = theForm.elements;

// if the textbox objects name = is the same as
// the form element called item 's value
if (textbox.name == theForm.item.value) {

// set the var 'location' to the textbox objects value
location = textbox.value;
}
}

// if the first letter is 'M'
if (location.substring(0,1)=="M"){

// change the top framesets location href to the var 'location'
parent.location.href = location
}

thats about right i think.

simon
 
Simon,

Thanks.

One more question, what is the definition of an element on a form? Is this all input tags, or only those with a type equal to a certain value, like type=text?
 
it is all the inputs (text / password / radio / checkbox) not file!

all selects / all textfields

simon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top