Welcome to WebmasterWorld Guest from 54.163.65.181

Forum Moderators: open

Message Too Old, No Replies

send value javascript help

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

Junior Member

10+ Year Member

joined:June 2, 2003
posts:113
votes: 0


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

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

Senior Member

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

joined:Mar 20, 2002
posts:3732
votes: 0


What is the name and id of the text field?
2:13 am on June 3, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:June 2, 2003
posts:113
votes: 0


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

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

Senior Member

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

joined:Mar 15, 2002
posts:6807
votes: 0


Try this:

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

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

Junior Member

10+ Year Member

joined:June 2, 2003
posts:113
votes: 0


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;

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

Senior Member

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

joined:Mar 20, 2002
posts:3732
votes: 0


How many text boxes are in the form and what number it is in order?
3:29 am on June 3, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:June 2, 2003
posts:113
votes: 0


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?

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

Junior Member

10+ Year Member

joined:June 2, 2003
posts:113
votes: 0


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

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

Senior Member

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

joined:Mar 20, 2002
posts:3732
votes: 0


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.

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

Junior Member

10+ Year Member

joined:June 2, 2003
posts:113
votes: 0


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>

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

Senior Member

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

joined:Mar 20, 2002
posts:3732
votes: 0


If it is the fourth element then it should have a 3 in the brackets. That should work.
3:15 pm on June 3, 2003 (gmt 0)

Preferred Member

10+ Year Member

joined:Apr 22, 2003
posts:441
votes: 0


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?
3:23 pm on June 3, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:June 2, 2003
posts:113
votes: 0


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

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

Preferred Member

10+ Year Member

joined:Apr 22, 2003
posts:441
votes: 0


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.

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

Senior Member

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

joined:Mar 20, 2002
posts:3732
votes: 0


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.
5:17 pm on June 3, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:June 2, 2003
posts:113
votes: 0


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

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

Junior Member

10+ Year Member

joined:June 2, 2003
posts:113
votes: 0


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]

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

Junior Member

10+ Year Member

joined:June 2, 2003
posts:113
votes: 0


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

Shawn

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

Preferred Member

10+ Year Member

joined:Apr 22, 2003
posts:441
votes: 0


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.