Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

javascript popup problem (values....)

Options
  • 27-03-2003 12:02pm
    #1
    Closed Accounts Posts: 59 ✭✭


    Hey all,

    I have a javascript popup window with a select statement:

    <SCRIPT TYPE="text/javascript" LANGUAGE="JavaScript"><!--
    function transfer() {
    opener.transferData = document.sendDataBack.fieldToTransfer.value;
    opener.restart();
    self.close;
    }

    //--></SCRIPT>

    <form name="sendDataBack" >
    <INPUT TYPE="button" VALUE="Select" onClick="transfer()"> <SELECT SIZE="-1" NAME="fieldToTransfer" VALUE="" >
    <OPTION>value I want passed back</OPTION></SELECT>
    <!--<input TYPE="text"NAME="fieldToTransfer" VALUE="" SIZE="10">-->
    </form>

    I want the selected value to be sent back to the parent window
    and be set as the value in the input field:
    this is the parent window:

    <SCRIPT LANGUAGE = "JavaScript">
    function restart() {
    document.myform.transferField.value = transferData;
    mywindow.close();
    }

    function popupWindow() {
    mywindow=open('popup.htm','myname','resizable=no,width=350,height=270');
    mywindow.location.href = 'searchdata.htm';
    if (mywindow.opener == null) mywindow.opener = self;
    }
    </SCRIPT>

    <form name="myform">
    <INPUT TYPE="TEXT" NAME ="transferField" VALUE ="" MAXLENGTH=0>
    <INPUT class="formelement2" TYPE="Button" NAME ="" VALUE ="Select" OnClick="popupWindow()">
    </form>

    This works fine if the input type in the popup is text type input but I cannot get it to work with a select statement. I have looked on google and I know it has something to do with 'selectedItem' or 'selectedValue' or something but i am new to javascript and don't really understand it!!

    Can anybody help me ou??
    Thanks
    Fi**

    ps: hope my code is clear enough!!


Comments

  • Registered Users Posts: 3,886 ✭✭✭cgarvey


    Your options don't have any values, just labels, is that what you want?

    The select object is an array of option objects. To determine which option in this array is selected use the ".selectedIndex" property of the select object .. see sample code below

    To store the label (the text you see in the dropdown list), you can accessit as in the third alert below ...
    <script type="text/javascript">
      var selObj = document.forms['sendDataBack'].elements['fieldToTransfer'];
      var selInd = selObj.selectedIndex;
    
      alert( "Selected index is " + selInd );
      alert( "HTTP value posted for this option is " + selObj[selInd].value );
      alert( "Label of the selected option is " + selObj[selInd].text );
    </script>
    
    


Advertisement