Retrieve Datagrid Element Index by Name

J

John Walker

Hi,
I have two datagrids within one form and I need to do some client-side
validation on the second grid when the user changes the value in drop down
list which is a control in the second grid. For performance reasons I want
javascript to loop through only the second grid when the drop down list event
is fired, and in order to do this I had planned to put a hidden label between
the two datagrids and have javasript start looping within the form using the
hidden label as the starting point.
I know how to get the form element names from the index(see code below), but
need to know if I can get the element index from the element name. My plan
is to get the index value of the hidden label and start looping from there.
Please let me know if it is possible to get the index value from the element
name and also if this seems like an ok plan.

dml=document.Form1;
len = dml.elements.length;
var i = 0;
while(i < len)
{
alert(dml.elements.name);
i = i + 1;
}

Thanks, John
 
P

Phillip Williams

If I attempt to get a collection of objects on an HTML page using JavaScript
I would write a function like this:

function FindControls(ControlID, TagName)
{
var ret= new Array();
var aControls = document.getElementsByTagName(TagName);
if (aControls)
{ for (var i=0; i < aControls.length ; i++)
{
if (aControls.id.lastIndexOf(ControlID) == aControls.id.length -
ControlID.length)
{
ret.push(aControls);
}
}
}
return ret;
}


Which you can call for any type of objects by passing the controlID and the
tag type:
function DisplayListsWithID_ddlList()
{

var controls = FindControls("ddlList","select");
alert(controls.length); //this gives how many on the page
for (var i=0;i< controls.length;i++)
{
//this allows you to loop through them and
//process any thing on them
alert(controls.id);
}

}
 
J

John Walker

Phillip,

It looks like FindControls will return an array of controls which have the
specified tag name. I'm not sure if that's what I need. I think what I need
is ability to loop through all the elements in only one of two tables in a
form. If Form1 has two tables, Table1 and Table2, is there a way to tell
javascript to only loop through Table2 when we're doing our validation? I
need to skip Table1 altogether because of performance issues.

Thanks again,
John

Phillip Williams said:
If I attempt to get a collection of objects on an HTML page using JavaScript
I would write a function like this:

function FindControls(ControlID, TagName)
{
var ret= new Array();
var aControls = document.getElementsByTagName(TagName);
if (aControls)
{ for (var i=0; i < aControls.length ; i++)
{
if (aControls.id.lastIndexOf(ControlID) == aControls.id.length -
ControlID.length)
{
ret.push(aControls);
}
}
}
return ret;
}


Which you can call for any type of objects by passing the controlID and the
tag type:
function DisplayListsWithID_ddlList()
{

var controls = FindControls("ddlList","select");
alert(controls.length); //this gives how many on the page
for (var i=0;i< controls.length;i++)
{
//this allows you to loop through them and
//process any thing on them
alert(controls.id);
}

}

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


John Walker said:
Hi,
I have two datagrids within one form and I need to do some client-side
validation on the second grid when the user changes the value in drop down
list which is a control in the second grid. For performance reasons I want
javascript to loop through only the second grid when the drop down list event
is fired, and in order to do this I had planned to put a hidden label between
the two datagrids and have javasript start looping within the form using the
hidden label as the starting point.
I know how to get the form element names from the index(see code below), but
need to know if I can get the element index from the element name. My plan
is to get the index value of the hidden label and start looping from there.
Please let me know if it is possible to get the index value from the element
name and also if this seems like an ok plan.

dml=document.Form1;
len = dml.elements.length;
var i = 0;
while(i < len)
{
alert(dml.elements.name);
i = i + 1;
}

Thanks, John
 
P

Phillip Williams

Hello John,

When you say "all of the elements" you probably have different processes to
apply to different controls, i.e. one process for dropdownlists, one for
tablecells and so on. If you have dropdownlists in table2 that you gave a
specific ID then you can find them first using the FindControls function then
do another set of controls of the same type and so on. The FindControls
functions (below) only picks up a set of controls that have the same ID (I
did it specifically this way to fit a templated control that emits the same
control as many times as there are items).

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


John Walker said:
Phillip,

It looks like FindControls will return an array of controls which have the
specified tag name. I'm not sure if that's what I need. I think what I need
is ability to loop through all the elements in only one of two tables in a
form. If Form1 has two tables, Table1 and Table2, is there a way to tell
javascript to only loop through Table2 when we're doing our validation? I
need to skip Table1 altogether because of performance issues.

Thanks again,
John

Phillip Williams said:
If I attempt to get a collection of objects on an HTML page using JavaScript
I would write a function like this:

function FindControls(ControlID, TagName)
{
var ret= new Array();
var aControls = document.getElementsByTagName(TagName);
if (aControls)
{ for (var i=0; i < aControls.length ; i++)
{
if (aControls.id.lastIndexOf(ControlID) == aControls.id.length -
ControlID.length)
{
ret.push(aControls);
}
}
}
return ret;
}


Which you can call for any type of objects by passing the controlID and the
tag type:
function DisplayListsWithID_ddlList()
{

var controls = FindControls("ddlList","select");
alert(controls.length); //this gives how many on the page
for (var i=0;i< controls.length;i++)
{
//this allows you to loop through them and
//process any thing on them
alert(controls.id);
}

}

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


John Walker said:
Hi,
I have two datagrids within one form and I need to do some client-side
validation on the second grid when the user changes the value in drop down
list which is a control in the second grid. For performance reasons I want
javascript to loop through only the second grid when the drop down list event
is fired, and in order to do this I had planned to put a hidden label between
the two datagrids and have javasript start looping within the form using the
hidden label as the starting point.
I know how to get the form element names from the index(see code below), but
need to know if I can get the element index from the element name. My plan
is to get the index value of the hidden label and start looping from there.
Please let me know if it is possible to get the index value from the element
name and also if this seems like an ok plan.

dml=document.Form1;
len = dml.elements.length;
var i = 0;
while(i < len)
{
alert(dml.elements.name);
i = i + 1;
}

Thanks, John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top