Home All Groups Group Topic Archive Search About

Retrieve Datagrid Element Index by Name

Author
10 Nov 2005 7:54 PM
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[i].name);
    i = i + 1;
    }

Thanks, John

Author
10 Nov 2005 9:50 PM
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[i].id.lastIndexOf(ControlID) == aControls[i].id.length -
ControlID.length)
            {
                ret.push(aControls[i]);
            }
        }
    }
    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[i].id);
                   }

}

Show quoteHide quote
"John Walker" wrote:

> 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[i].name);
>     i = i + 1;
>     }
>
> Thanks, John
>
Author
11 Nov 2005 5:57 PM
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

Show quoteHide quote
"Phillip Williams" wrote:

> 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[i].id.lastIndexOf(ControlID) == aControls[i].id.length -
> ControlID.length)
>             {
>                 ret.push(aControls[i]);
>             }
>         }
>     }
>     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[i].id);
>                    }
>
> }
>
> --
> HTH,
> Phillip Williams
> http://www.societopia.net
> http://www.webswapp.com
>
>
> "John Walker" wrote:
>
> > 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[i].name);
> >     i = i + 1;
> >     }
> >
> > Thanks, John
> >
Author
11 Nov 2005 6:37 PM
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).

Show quoteHide quote
"John Walker" wrote:

> 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" wrote:
>
> > 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[i].id.lastIndexOf(ControlID) == aControls[i].id.length -
> > ControlID.length)
> >             {
> >                 ret.push(aControls[i]);
> >             }
> >         }
> >     }
> >     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[i].id);
> >                    }
> >
> > }
> >
> > --
> > HTH,
> > Phillip Williams
> > http://www.societopia.net
> > http://www.webswapp.com
> >
> >
> > "John Walker" wrote:
> >
> > > 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[i].name);
> > >     i = i + 1;
> > >     }
> > >
> > > Thanks, John
> > >