Welcome to WebmasterWorld Guest from 54.147.10.72

Forum Moderators: open

Message Too Old, No Replies

send value javascript help

     

soquinn

6:35 pm on Jun 2, 2003 (gmt 0)

10+ Year Member



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. I'm opening the popup window with:

<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

korkus2000

11:56 pm on Jun 2, 2003 (gmt 0)

WebmasterWorld Senior Member korkus2000 is a WebmasterWorld Top Contributor of All Time 10+ Year Member



What is the name and id of the text field?

soquinn

2:13 am on Jun 3, 2003 (gmt 0)

10+ Year Member



Hello, the id and name are basically the same except the id does not have the []'s.

Example:

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

DrDoc

2:15 am on Jun 3, 2003 (gmt 0)

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Try this:

window.opener.document.getElementById['field_1'].value = selvalue;

soquinn

2:31 am on Jun 3, 2003 (gmt 0)

10+ Year Member



Hi DrDoc, hmmm... that did not work?

window.opener.document.getElementById['field_1'].value = selvalue;

it did not work when I referenced the form name either like this:

window.opener.document.my_form1.getElementById['field_1'].value = selvalue;

korkus2000

3:02 am on Jun 3, 2003 (gmt 0)

WebmasterWorld Senior Member korkus2000 is a WebmasterWorld Top Contributor of All Time 10+ Year Member



How many text boxes are in the form and what number it is in order?

soquinn

3:29 am on Jun 3, 2003 (gmt 0)

10+ Year Member



Hi korkus2000,

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?

soquinn

1:57 pm on Jun 3, 2003 (gmt 0)

10+ Year Member



The forms names also do not have quotation marks they are like:

<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

korkus2000

2:37 pm on Jun 3, 2003 (gmt 0)

WebmasterWorld Senior Member korkus2000 is a WebmasterWorld Top Contributor of All Time 10+ Year Member



getElementById should work. That is very odd.

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.

soquinn

3:02 pm on Jun 3, 2003 (gmt 0)

10+ Year Member



Hi korkus2000,

if it is the 4th element would it be written like:

<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function sendValue(s){
var selvalue = s.options[s.selectedIndex].value;
window.opener.document.my_form1.element[4].value = selvalue;
window.close();
}
// End -->
</script>

korkus2000

3:14 pm on Jun 3, 2003 (gmt 0)

WebmasterWorld Senior Member korkus2000 is a WebmasterWorld Top Contributor of All Time 10+ Year Member



If it is the fourth element then it should have a 3 in the brackets. That should work.

AWildman

3:15 pm on Jun 3, 2003 (gmt 0)

10+ Year Member



What browser are you using to check your Javascript? Are you trying to make it work cross browser? Your problems may stem from the fact that you aren't using the right properties for the browser you are using. getElementById should work in IE but won't if you are checking it in NN 4.
Is your current code throwing an error or simply not performing as expected?

soquinn

3:23 pm on Jun 3, 2003 (gmt 0)

10+ Year Member



I'm using IE 6 to test and I'm not getting any errors, it's just not performing as expected?

It works fine when I reference a text field name with no []'s in it but trying to reference an id or element has yet to work?

Shawn

AWildman

3:44 pm on Jun 3, 2003 (gmt 0)

10+ Year Member



I'm surprised that the last bit of code you'd had didn't work on ie. How about this: get a reference to the form object's element array. In a for loop check the name or id given in the array against the one you are looking for. If equal,set the object equal to your selvalue.

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.

korkus2000

4:48 pm on Jun 3, 2003 (gmt 0)

WebmasterWorld Senior Member korkus2000 is a WebmasterWorld Top Contributor of All Time 10+ Year Member



You will have to rename your elements without brackets. When you are calling an object reference you can't have brackets because those are reserved for arrays.

soquinn

5:17 pm on Jun 3, 2003 (gmt 0)

10+ Year Member



Thanks for all your help korkus2000.

Unfortunately, renaming the elements without brackets means rewriting the application and database. I guess I'm out of luck on an easy JavaScript solution:-)

Thanks again,

Shawn

soquinn

9:06 pm on Jun 5, 2003 (gmt 0)

10+ Year Member



Folks, I'm back after some great support from AWildman. We are now stuck, value is not beeing pased back to the parent form, so maybe someone else has the knowledge to make this work. Here is a recap:

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]

soquinn

10:04 pm on Jun 5, 2003 (gmt 0)

10+ Year Member



Sorry for posting the urls... I can sticky mail someone if you think you can help.

Shawn

AWildman

12:06 pm on Jun 6, 2003 (gmt 0)

10+ Year Member



FYI - We did at one point get "undefined" to appear in the parent form. So the only value we're not getting is the proper reference to the option selected in the child window's form.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month