SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Jan 2005
    Location
    Malaysia
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Setting focus on specific SELECT option

    Hi, my problem seems simple enough, but I can't seem to find any solution to it.

    I have a SELECT element in a form like this:

    Code:
    <td>
    <select name="q_type" onChange="writeTxtBox(this.value);">
    <option value="0">(select an option)</option>
    <option value="SELECT">SELECT</option>
    <option value="DELETE">DELETE</option>
    <option value="INSERT">INSERT</option>
    <option value="ORDER BY">ORDER BY</option>
    </select>
    </td>
    When a user selects a value, the writeTxtBox() function is called:

    Code:
    function writeTxtBox(strVal) {
    
    var queryTxt = new String();
    
     if (strVal != "0") {
    	
    	queryTxt = strVal + " ";
    	
    	document.myForm.txtBox.value += queryTxt;
    
     } // if
    
    } // writeTextArea()
    and the selected option will be added to the text input box.

    What I want to do is, as soon as the user has clicked on an option and the value is written to the text box, the focus is then set back to the first option in the SELECT element, i.e. the one which says (select an option).

    But so far I've only been able to set the focus on the SELECT element rather than on a specific option.

    Does anyone have any ideas on how I can accomplish this?

    Thanks!

    rishi

  2. #2
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    You can use the selected attribute to return the focus to a specific option.

    Code:
    function writeTxtBox(strVal) {
    
    var queryTxt = new String();
    
     if (strVal != "0") {
    	
    	queryTxt = strVal + " ";
    	
    	document.myForm.txtBox.value += queryTxt;
            document.myForm.selectList.options[0].selected = true;
    
     } // if
    
    } // writeTextArea()
    Yours, Erik.
    ERIK RIKLUND :: Yes, I've been gone quite a while.

  3. #3
    SitePoint Member
    Join Date
    Jan 2005
    Location
    Malaysia
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Whoa! That was fast! Thanks lilleman! You've just made a newbie very very happy!

    Quote Originally Posted by lilleman
    Hi,

    You can use the selected attribute to return the focus to a specific option.

    Code:
    function writeTxtBox(strVal) {
    
    var queryTxt = new String();
    
     if (strVal != "0") {
    	
    	queryTxt = strVal + " ";
    	
    	document.myForm.txtBox.value += queryTxt;
            document.myForm.selectList.options[0].selected = true;
    
     } // if
    
    } // writeTextArea()
    Yours, Erik.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •