Click to See Complete Forum and Search --> : Update select drop down box in parent from child


wakeboarder_ben
07-28-2005, 01:01 PM
Here is the situation:

i have a parent page main.asp, and a popup child window update.asp. i would like the user to be able to open the popup from the parent page, input a text value into update.asp, and pass this value back to the parent page, and insert it into a select box (at the end).

I can update a text field(shipID) fine, but not a select box...

Here's the code i currently have which doesn't work:

function updateParent() {
var mySize = opener.salesSurvey.account.options.length;
alert("1 = "+orgName+", 2 = "+orgID + " selectboxsize = "+mySize);

opener.salesSurvey.shipID.value = "ben";

opener.salesSurvey.account.options[mySize+1].text = "newONE";
opener.salesSurvey.account.options[1].selected = "true";
alert("closing");
window.close();
}

I'd appreciate any help.

thanks.

Jonny Lang
07-29-2005, 06:50 AM
The function for inserting options must be in the parent, and is called from the popup.


Main document:

<HTML>
<Head>
<Script Language=JavaScript>

var tmpWin = "";

function popWin(){

tmpWin = window.open('pop2.html',"");
}

function insertOpt(isData){

isList = document.forms.formcost.service;
isData = new Option(isData,isData);
isList.add(isData,isList.options.length);
}

function dispVal(isVal){

alert(isVal);
}

</Script>
</Head>
<Body>
<Form name='formcost'>
<Select name='service' onchange="dispVal(this.value)">
<option value='0' selected> Make a selection </option>
<option value='Text 1' > Text 1 </option>
<option value='Text 2' > Text 2 </option>
</Select>
</Form>
<input type=button value="Open Window" onclick="popWin()"><br><br>
</Body>
</HTML>


pop2.html:

<HTML>
<Head>
<Script Language=JavaScript>

function newOpt(){

opener.insertOpt(document.forms.popForm.dynOpt.value);
self.close();
}

</Script>
</Head>
<Body>
<Form name='popForm'>
<input type=text size=10 name='dynOpt'>
<input type=button value="Update" onclick="newOpt()"><br><br>
</Form>
</Body>
</HTML>

wakeboarder_ben
07-29-2005, 09:14 AM
Thanks alot for your help!! I have one more minor question: how do i make the new option selected when the popup closes?

Jonny Lang
07-29-2005, 09:33 AM
One more line, in this function:

function insertOpt(isData){

isList = document.forms.formcost.service;
isData = new Option(isData,isData);
isList.add(isData,isList.options.length);
isList.selectedIndex = isList.length-1;
}

wakeboarder_ben
07-29-2005, 10:37 AM
Everything is working perfectly.

I have another question... I am a bit unfamiliar with the syntax of javascript, is there a way that i could pass the select name as well?

ie:

//--- update select box field in parent form ---
function insertOpt(mySelect, myName, myValue){
myList = document.forms."+mySelect+";
myData = new Option(myName, myValue);
myList.add(myData, myList.options.length);
myList.selectedIndex = myList.length-1;
}

this doesnt seem to work. I also am wondering when double quotes are required as opposed to using single quotes?

thanks,
Ben

wakeboarder_ben
07-29-2005, 10:44 AM
actually, i think i found it:

//--- update select box field in parent form ---
function insertOpt(mySelect, myName, myValue){
myList = document.getElementById(mySelect);
myData = new Option(myName, myValue);
myList.add(myData, myList.options.length);
myList.selectedIndex = myList.length-1;
}

Jonny Lang
07-29-2005, 10:46 AM
Passing both attributes, text and value:

function insertOpt(isText,isVal){

isList = document.forms.formcost.service;
isData = new Option(isText,isVal);
isList.add(isData,isList.options.length);
}

The isText and isVal may need to be reversed, I can't recall the correct order at the moment.

You set a variable to equal the the list, as shown in the first line:

theList = document.forms.formName.selectListName; In the above, the NAME of the form is formcost, and the NAME of the select list is service.
.....



function newOpt(){

opener.insertOpt(document.forms.popForm.dynOpt.value,'this is the value');
self.close();
}


There are only one or two instances when double quotes are required. And then, just use single quotes inside the double quotes. Othewise, just be consistent.

When calling an onclick to a function and passing a variable to it, then double quotes must be used.

onclick="doThis('something')"


EDITED

wakeboarder_ben
07-29-2005, 11:15 AM
thanks for your help.

Jonny Lang
07-29-2005, 11:18 AM
You're welcome.