...

View Full Version : send value javascript help



soquinn
06-10-2003, 08:02 PM
Hello folks, Iím not familiar with editing JavaScript code and was wondering if anyone can help? Iím trying to populate a text field in a form with values from a popup or DHTML window. Similar to a popup calendar selection function.

The text field reference I want populated from a popup list of items looks like this:

<input type=text name=b[optional_field_1] id=optional_field_1 value="">

I can't seem to reference the "name=" in the javascript because it has [] in it. So I tried to reference the id.

I call the popup window with:

<a href="#" onClick="window.open('http://mysite.com/popup-file.php','popuppage','width=550,height=500,top=20,left=100,scrollbars=1,resizable=1,toolbar=1,locat ion=1,menubar=1');">Select Make</a>

and use this to send the selected value back to the text field:

<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 to send back the value:

<input type=button value="Add Make" onClick="populate_parent_formfield(document.selectform.elements['selectmenu'].selectedIndex.value)" name="button">

This is as far as I could get with help from others. Any suggestions on what might be missing would be greatly appreciated. Can idís be referenced or is there another/better way to send the value back to the text field?

Thanks in advance,

Shawn

mordred
06-11-2003, 12:18 AM
Originally posted by soquinn
I can't seem to reference the "name=" in the javascript because it has [] in it. So I tried to reference the id.


You don't have to take the workaround, you can reference names with brackets in them:

<input type="text" name="b[optional_field_1] " id="optional_field_1" value="" />

reference it like

var val = document.forms["nameOfForm"].elements["b[optional_field_1]"].value;

Hope that helps anything, I haven't looked closely enough into your following problems, but as I said, I doubt this workaround is necessary at all.

soquinn
06-11-2003, 04:11 PM
Thanks for the relpy mordred... no one has mentioned this to me before... can I ask you to please send me an example of all the code that would replace this:

<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>


Thanks,

Shawn



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum