homepage Welcome to WebmasterWorld Guest from 54.163.72.86
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
send value javascript help
soquinn




msg:1487242
 6:35 pm on Jun 2, 2003 (gmt 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

 

korkus2000




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

What is the name and id of the text field?

soquinn




msg:1487244
 2:13 am on Jun 3, 2003 (gmt 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="">

DrDoc




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

Try this:

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

soquinn




msg:1487246
 2:31 am on Jun 3, 2003 (gmt 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;

korkus2000




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

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

soquinn




msg:1487248
 3:29 am on Jun 3, 2003 (gmt 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?

soquinn




msg:1487249
 1:57 pm on Jun 3, 2003 (gmt 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

korkus2000




msg:1487250
 2:37 pm on Jun 3, 2003 (gmt 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.

soquinn




msg:1487251
 3:02 pm on Jun 3, 2003 (gmt 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>

korkus2000




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

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

AWildman




msg:1487253
 3:15 pm on Jun 3, 2003 (gmt 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?

soquinn




msg:1487254
 3:23 pm on Jun 3, 2003 (gmt 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

AWildman




msg:1487255
 3:44 pm on Jun 3, 2003 (gmt 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.

korkus2000




msg:1487256
 4:48 pm on Jun 3, 2003 (gmt 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.

soquinn




msg:1487257
 5:17 pm on Jun 3, 2003 (gmt 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

soquinn




msg:1487258
 9:06 pm on Jun 5, 2003 (gmt 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]

soquinn




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

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

Shawn

AWildman




msg:1487260
 12:06 pm on Jun 6, 2003 (gmt 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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved