capture text selected in a select tag

Discussion in 'JavaScript' started by cedtech31, Dec 29, 2007.

  1. #1
    I have a simple select tag on a form

    <select name="req" id="req' >
    <option value="1">James Young</option>
    <option value="2">Kebu Steward</option>
    <option value="3">Randy Clarke</option>
    </select>


    How can I capture the text of the selected item.
    example suppose "Kebu Steward" was selected by the user

    how could I capture "Kebu Steward" in one value then break it into two
    values
    var f_name = Kebu
    var l_name = Steward

    This is my first time with such and issue and I don't know how to approach the problem.

    Thanks
     
    cedtech31, Dec 29, 2007 IP
  2. cedtech31

    cedtech31 Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Found some reading material that helped solve the issue.

    
    <script type="text/javascript">
    function  passVals(){
            
    	//used to get text of selected item in select tag	
    	reqList = document.getElementById("req");
    	var full_name = reqList.options[reqList.selectedIndex].text;
    	
    	//separate full name in an array
    	var fl_name = full_name.split(" ");
    
    	//assign values to parent elements
    	window.opener.document.appts.req_f_name.value= fl_name[0];
    	window.opener.document.appts.req_l_name.value= fl_name[1];
    	window.close();
    }
    </script>
    
    Code (markup):
     
    cedtech31, Dec 29, 2007 IP