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

Dynamic form 'name' 1

Status
Not open for further replies.

Deleted

Technical User
Jul 17, 2003
470
US
Trying to get the field value to be dynamic. Any help much appreciated.

function update(name, value) {
window.opener.document.my_form.field[name].value = value;
}
 
Try this instead:

1) Be sure to have the id attribute of the field in question set, lets say to field_1, usually the same as the name attribute, in this case also field_1:
Code:
[...]
<input id="field_1" name="field_1" [...] />
[...]
<button onclick="update('field_1', 'test');" [...] />
[...]

2) Reference the field like so to change the value:
Code:
function update(s_name, s_value)
{
    window.opener.document.getElementById(s_name).value = s_value;
}

Take Care,
Mike
 
i'd rather this:

Code:
function update(name, value) {
    opener.document.forms['my_form'].elements[name].value = value;
}

if an id isn't necessary, "Deleted" might not have one.



*cLFlaVA
----------------------------
[tt]somebody set up us the bomb![bomb][/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
The reason I ALWAYS use the id attribute is because name is not necessarily unique.
Also, you may not always be accessing form elements, but want to change the values of other items that are not contained in a form.


Take Care,
Mike
 
values not within a form? are we still talking form elements? if you have form elements not within a form, then you have invalid html.

if you do have form elements in the same form with the same name, you can simply reference them using array notation:

Code:
opener.document.forms['my_form'].elements[name][red][0][/red].value = value;

a matter of preference, of course, but if you happen to have the need to loop through all form elements, this notation is definitely the better approach.



*cLFlaVA
----------------------------
[tt]somebody set up us the bomb![bomb][/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
I definately agree, when looping through from elements your option is the way to go.

I use update statements like this up update other page elements (not form elements) outside of a form, i.e. innerHTML, innerText, and so on.

Bottom line: its a matter of preference, and perhaps also a matter of standardizing the way you program your web application.

Take Care,
Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top