Forum Moderators: open
<a href="#" onclick="window.open('http://www.myurl.com/popup.php','popuppage','width=550,height=500,top=20,left=100');">Select Here</a>
and I have this code in the popup page:
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function sendValue(s){
var selvalue = s.options[s.selectedIndex].value;
window.opener.document.my_form1.b[field_1].value = selvalue;
window.close();
}
// End -->
</script>
but the [] seem to be a problem. All the field names have [] in them but I also have id’s like: “id=field_1” along with “name=b[field_1]”… can id’s be referenced or is there another/better way to send the value back to the text field?
Thanks in advance,
Shawn
There are about 6 text boxes, a few dropdowns, a few check boxes, it may be different as they are dynamic… but only 3 have id values attached like:
<input type=text name=b[field_1] id=field_1 value="">
<input type=text name=b[field_2] id=field_2 value="">
<input type=text name=b[field_3] id=field_3 value="">
The one with the popup reference is the first one: id=field_1. All of these fields are already coded in an 3rd party application so I thought it would be easier to tackle it from the JavaScript side rather then try to edit names, structure...etc.
The page also has two forms on it so I thought that might need to be referenced? The text fields do not have quotation marks like:
<input type=text name="b[field_1]" id="field_1" value="">
would that matter?
<form name=my_form1 action=system.php?a=1 method=post>
Is there another way to the populate a text field in a form with values from a javascript popup or DHTML window where the form field names have []'s around them and field id's do not?
Example:
<input type=text name=b[field_1] id=field_1 value="">
Thanks,
Shawn
When a document is loaded javascript creates arrays for each scriptable item forms, links, etc. So you can access all elements with the array value. Within your form you need to count how many form elements(not labels) are in that form. Javascript starts with 0 as the first number so you start counting at 0 for the first element. Element 1 is really element 0. So count the elements and use that inside the brackets like:
document.formname.element[3].value
This will give you the 4th element in that form.
The quotes shouldn't matter unless you are producing XHTML. I would add quotes, but it is not necissary.
var elarray = window.opener.document.my_form.elements;
for(var i = 0; i < elarray.length; i++)
{
if(elarray[i].id == "the name you're looking for")
{elarray[i].value = selvalue; break;}
}
I would lose the if statement the first time around and alert all the id's that appear 1) to be sure you've got a valid object reference and 2) to see what values are actually given so that you know if your value appears therein.
I have a test form here:
<sorry, no URLs>
The text field I want populated form a popup list of items looks like this:
<input type=text name=b[optional_field_1] id=optional_field_1 value="">
I can't reference the name in javascript because it has [] in it. So we tried to reference the id.
I call the popup window with:
<a href="#" onClick="window.open('http://usa.motoseller.com/popup-file.php','popuppage','width=550,height=500,top=20,left=100,scrollbars=1,resizable=1,toolbar=1,location=1,menubar=1');">Select Make</a>
and use this to send the selected value back:
<SCRIPT LANGUAGE="JavaScript">
function populate_parent_formfield(val)
{
alert("val: " + val)//make sure that it is what you chose in the pop-up window select box; remove once verified
var elarray = window.opener.document.classified_details_form.elements;
for(var i = 0; i < elarray.length; i++)
{
if(elarray[i].id == "optional_field_1")
{elarray[i].value = val; break;}
}
}
</script>
this is the popup window button:
<input type=button value="Add Make" onClick="populate_parent_formfield(document.selectform.elements['selectmenu'].selectedIndex.value)" name="button">
Any suggestions on what might be missing would be greatly appreciated.
Thanks,
Shawn
[edited by: tedster at 9:18 pm (utc) on June 5, 2003]