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

Adapting a function for use with repeated fields

Status
Not open for further replies.

JamesAlex

MIS
Joined
Mar 17, 2004
Messages
10
Location
CH
Hello,

I'm new to JavaScript, and am have written the following function, which is run at the onClick event of a checkbox, and will populate Textfield1 with the contents of Textfield (the purpose is to allow a user to complete a single comments text box and then apply that comment to multiple text boxes to save typing):
Code:
function PopulateTextBoxes()
	{
		if (document.form1.checkbox.checked)
			{
			document.form1.checkbox.value="true"
			document.form1.textfield1.value = document.form1.textfield.value
			}
		else 
			{
			document.form1.checkbox.value="false"
			document.form1.textfield1.value = ""
			}
	}

I have successfully extended the function to add the value of "textfield" to multiple named text boxes.

The problem is, I want to implement the function on a page which returns multiple rows from a database (using repeat region), where I have a comment text box for each row. Each field in a row is distinguised by a unique record ID appended to its name, e.g. <input type="text" name="comments<%= intRecID %>">. This means I can no longer just refer to the specific name of the field to update it within the function, as I don't know what the value of intRecID will be. Can anyone suggest how I might adapt the function to be able to refer to these field names?

Many thanks,

-James
 
You need *some* way of distinguishing the target fields - otherwise you'll never be able to achieve this.

For example, maybe you guarantee that the only form fields beginning with the word "comments" are the target fields (so no other field names begin with "comments"). Then, simply loop around all form fields, checking to see if the start of the name is "comments", and if so, populating them. You can loop around form fields and check the name like so:

Code:
var frm = document.forms['form1'].elements;
for (var loop=0; loop<frm.length; loop++) {
	if (frm[loop].name.indexOf('comments') == 0) {
		frm[loop].value = 'some value';
	}
}

Hope this helps,
Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Hi Dan,

that certainly makes sense to me - I will try adapting my code to include a loop and let you know the results!

Thanks for taking the time to respond to my question so quickly.

-James
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top