473,382 Members | 1,124 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Retrieving the ID names from within a form

CES
All,

Is their a way of iterating thru each tag within a form and returning the
value given in the id property, by that I mean the below html would return
the values idBoxOne, idBoxTwo, idBoxThree from the FormXX form.

<form name="FormXX" method="post" action="default.htm">
<input id="idBoxOne" name="bxOne" type="text">
<input id="idBoxTwo" name="bxTwo" type="text">
<input id="idBoxThree" name="bxThree" type="text">
</form>

Up to this point I've only used JavaScript to change CSS properties,
innerHTML and outerHTML so I'm rather new to the game.

Any help is always appreciated.

CES
Jul 20 '05 #1
3 6762
Lee
CES said:

All,

Is their a way of iterating thru each tag within a form and returning the
value given in the id property, by that I mean the below html would return
the values idBoxOne, idBoxTwo, idBoxThree from the FormXX form.

<form name="FormXX" method="post" action="default.htm">
<input id="idBoxOne" name="bxOne" type="text">
<input id="idBoxTwo" name="bxTwo" type="text">
<input id="idBoxThree" name="bxThree" type="text">
</form>

Up to this point I've only used JavaScript to change CSS properties,
innerHTML and outerHTML so I'm rather new to the game.

Any help is always appreciated.


<html>
<head>
<script type="text/javascript">
function listIDs(f){
var msg="";
for(var i=0;i<f.elements.length;i++){
msg+=i+": id: "+f.elements[i].id
+" name: "+f.elements[i].name
+" type: "+f.elements[i].type
+"\n";
}
alert(msg);
}
</script>
</head>
<body onload="listIDs(document.forms['FormXX'])">
<form name="FormXX" method="post" action="default.htm">
<input id="idBoxOne" name="bxOne" type="text">
<input id="idBoxTwo" name="bxTwo" type="text">
<input id="idBoxThree" name="bxThree" type="text">
</form>
</body>
</html>

Jul 20 '05 #2
CES
All,
Just incase anyone is interested this is the final code. I needed to change
the input id in the html below from id="idBoxOne" to id="id_BoxOne".

This returns an array of any form tag that has an id that starts with id_ .
The only real change made to Lee's code was insted of passing the name to
the function using listIDs(document.forms['FormXX'])", I got the name of the
first form by using document.forms[0].name, so the onLoad event would now be
onLoad=listIDs().

Thanks for the starting point Lee.

CES

function listIDs(){
var a = document.forms[0].name; // Retrevies the name of the first form
on the page
var f = document.forms[a];
var nArray = new Array(); //Initializes the array
var z = 0; //Sets the array index number
for(var i = 0;i < f.elements.length;i++){ //Loops thru all ellements in
the form
var x = f.elements[i].id; // Assigns the value of the forms element to
var
var y = x.split("_"); //Determines if the Element is to be tracked, all
tracked elements have the "id_"
if(y[0] == "id"){ //If the left portion of the is is id then it adds
right item to array
nArray[z] = y[1];
z++ // increments by one
}
}
return nArray;
}

CES
"Lee" <RE**************@cox.net> wrote in message
news:bm********@drn.newsguy.com...
CES said:

All,

Is their a way of iterating thru each tag within a form and returning the
value given in the id property, by that I mean the below html would returnthe values idBoxOne, idBoxTwo, idBoxThree from the FormXX form.

<form name="FormXX" method="post" action="default.htm">
<input id="idBoxOne" name="bxOne" type="text">
<input id="idBoxTwo" name="bxTwo" type="text">
<input id="idBoxThree" name="bxThree" type="text">
</form>

Up to this point I've only used JavaScript to change CSS properties,
innerHTML and outerHTML so I'm rather new to the game.

Any help is always appreciated.


<html>
<head>
<script type="text/javascript">
function listIDs(f){
var msg="";
for(var i=0;i<f.elements.length;i++){
msg+=i+": id: "+f.elements[i].id
+" name: "+f.elements[i].name
+" type: "+f.elements[i].type
+"\n";
}
alert(msg);
}
</script>
</head>
<body onload="listIDs(document.forms['FormXX'])">
<form name="FormXX" method="post" action="default.htm">
<input id="idBoxOne" name="bxOne" type="text">
<input id="idBoxTwo" name="bxTwo" type="text">
<input id="idBoxThree" name="bxThree" type="text">
</form>
</body>
</html>

Jul 20 '05 #3
"CES" <No**@noSpam.com> wrote in message
news:g_********************@speakeasy.net...
<snip>
This returns an array of any form tag that has an id that starts
with id_ . The only real change made to Lee's code was insted of
passing the name to the function using listIDs(document.forms[
'FormXX'])", I got the name of the first form by using
document.forms[0].name, so the onLoad event would now be
onLoad=listIDs(). <snip> function listIDs(){
var a = document.forms[0].name; // Retrevies the name of the
// first form on the page
var f = document.forms[a];
Is there any point in doing this? The only circumstances where
document.forms[0] will not refer to the same object as
document.forms[document.forms[0].name] is when more than one form on the
page has the same name, and in that case the rest of your code is going
to fall over anyway. Wouldn't it make more sense to use - var f =
document.forms[0]; -. However, every use of - f- is followed with -
..elements - so why not assign a reference to the fomr's elements
collection to a local variable instead of a reference to the form
itself:-

var els = document.forms[0].elements;
.. . .
for(var i = 0;i < els.length;i++){
.. . .
var x = els[i].id;
var nArray = new Array(); //Initializes the array
Array literal notation:-

var nArray = []; //New empty array.
var z = 0; //Sets the array index number
Maybe you don't need - var z -.
for(var i = 0;i < f.elements.length;i++){ //Loops thru all ellements //in the form
var x = f.elements[i].id; // Assigns the value of
// the forms element to var
var y = x.split("_"); //Determines if the Element is to be tracked, //all tracked elements have the "id_"
if(y[0] == "id"){ //If the left portion of the is is id then
//it adds right item to array
nArray[z] = y[1];
z++ // increments by one

<snip>

Could be combined into:-

nArray[z++] = y[1];

-but you could use:-

nArray[nArray.length] = y[1];

- and not need to keep track of how long the array is.

Richard.
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Josep | last post by:
Hi, I'd like to poll a database and get the table contents, as well as the field names from that table. I've been to php.net but this time I cannot find something helpful. I can get the data,...
2
by: Larry Neylon | last post by:
Hi, What I am trying to do is to work out which part of a HTML page contained within an IFRAME has been selected. I have complete control over both the main window and the content of the IFRAME...
35
by: NickName | last post by:
I understand it's easy to list all saved queries of a given Access database via Msysobjects system table. However, I have not seen any posting over this NG or other similar ones that also include...
10
by: Girish | last post by:
Hi Everyone, I am passing a form to a php script for further processing. I am able to retrieve the last value set for that given form variable using $variable=$_REQUEST;
7
by: rfinch | last post by:
Very new to this but using the MS working with dynamics CRM 3.0 book to run web application to retrieve lead records from CRM 3.0. Have followed the book instructions on page 380-382. But am...
5
by: Randy Smith | last post by:
Hi ALL, I wonder if anyone has been using n-tier to bind to a GridView control by using the ObjectDataSource. This is our first OOP web application, and we have no tables. Right now we are...
1
by: Chris L. | last post by:
Hi, I'm trying to set up a generic mechanism that would allow the following: 1: Run <this_procedure> 2: After that, run <this_review_procedure> 3: If it returns non-zero, open <this_form> ...
2
by: teddarr | last post by:
I want to write a cookie that will hold purchase information. The name of this cookie will be a julian date/time. How do I go about retrieving all the cookies within a certain time period? ...
0
bmallett
by: bmallett | last post by:
First off, i would like to thank everyone for any and all help with this. That being said, I am having a problem retrieving/posting my dynamic form data. I have a form that has multiple options...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.