Forum Moderators: open

Message Too Old, No Replies

Setting parent window select list value?

         

joedub

5:23 pm on Apr 12, 2007 (gmt 0)

10+ Year Member



Hello,

I'm having trouble at the moment with a popup window setting a select field value to selected, contained within a form on it's parent window.


Form On Parent Page:

<form id="myform">
<select name="form_fields[distance]">
<option value="10">distance 1
<option value="11">distance 2
<option value="12">distance 3
</select>
</form>


Javascript On Popup:

function setParentDistance( val )
{
// window.opener.myform.elements["form_fields[distance]"].selectedIndex = val-1;

var selectedDis = window.opener.getElementById( form_fields['distance'][val] );
selectedDis.selected = true;

window.close();
}

// called on popup via
//setParentDistance( 2 );
setParentDistance( 12 );

The commented out javascript line works when the function is called using the correlating selected index value, but i'm trying to rearrange so that it will work based on the option value?

Any help much appreciated

joedub

11:44 am on Apr 13, 2007 (gmt 0)

10+ Year Member



Ended up going with:


function setParentDistance( val )
{
// fetch select from parent window
var selectedDis = window.opener.document.getElementById( 'form_fields[distance]' );

// loop through select options, if the value matches val, use this selected index!
for( var i=0; i<selectedDis.options.length; i++ )
{
testval = selectedDis[i].value;

if( testval == val )
{
selectedDis[i].selected = true;
}
}

window.close();
return false;
}

As a side note - had everything working in IE and Opera, but struggled with Firefox. The problem was caused by the line 'window.opener.document.getElementById' returning an object in IE/opera and Null in firefox. Forgot to add an id="" tag to the select tag. IE/opera were ignoring the missing id tag and finding the element using its name instead...