Link to home
Create AccountLog in
Avatar of lokiette
lokiette

asked on

Dropdown list javascript focus issue

I have a dropdown list and a text box in a form in that order.  On the blur of the dropdown, I evaluate the contents (in javascript) and in a certian case pop up a message box and set the focus back to the dropdown to force the user to change the value.

My problem is, when there is text in the textbox the text gets highlighted when the blur of the dropdown occurs.  When I set the focus back to the dropdown (in script) the text in the textbox stays highlighted.  

I realize if the dropdown list was a textbox then I could use the textbox.select().  However, since it is a dropdown this function is not available.  I've tried everything I can think of with no avail... Any help would be MUCH appriciated!!!

Here is some sample code - this is not the actual code but you'll get the gist:

<script language="javascript">
   function EvaluateMe()
   {
    if (document.getElementById("dropdown1").value == "A")
    {
        window.alert("Invalid entry.");
        document.getElementById("dropdown1").focus();
     }
   }
</script>

<asp:dropdownlist id="dropdown1" runat=server onBlur="EvaluateMe()">
 <asp:ListItem text="A" value="A"></asp:ListItem>
 <asp:ListItem text="B" value="B"></asp:ListItem>
</asp:dropdownlist>

<asp:textbox id="textbox1" runat=server text="textbox text"></asp:textbox>
ASKER CERTIFIED SOLUTION
Avatar of Russell2566
Russell2566
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of lokiette
lokiette

ASKER

Wow, I never thought about that!  It definately works for a short term solution but I really need something not so hackish.  The fields on the form tend to change so I would have to add that onBlur to multiple fields, which seems a little crazy.  

One solution I am considering is using the onChange vs. onBlur but I'm not sure this is the best solution for my application since then the script will only be called when the value changes vs every time the field loses focus.  I'm still looking for alternatives and would like to keep it confined to the one function.
There really isn't another way that I can come up with. There is no built in way to find out which element is currently selected. so another option would be to simply have an onFocus="currentObj=this;" in all your fields; which again is yes cheeky...

I would recomend bagging the onBlur and either do it onChange or off of the submit/next button.
Forced accept.

Computer101
EE Admin