How to give focus on List box .

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Vini171285
    New Member
    • Apr 2008
    • 60

    How to give focus on List box .

    Hi friends,

    how to give focus on list box using javascript in ASP....
    actually i could give focus on the list box...but after clicking the alert button,it focuses on the list box,,,and before doing any change it immediately passes to the next page...
    Please guide me to avoid this problem...!!

    Thanx in Advance!!
  • DrBunchman
    Recognized Expert Contributor
    • Jan 2008
    • 979

    #2
    Hi Vini171285,

    Can you give an example of your code please so we can see what you've done so far.

    Dr B

    Comment

    • Vini171285
      New Member
      • Apr 2008
      • 60

      #3
      Hi DrBunchman
      This is my code.My problem is if listbox is not selected there shd be a alert msg,and focus shd go on the listbox.But after alert msg is clicked,next page is immediately loaded and listbox remains empty.
      Code:
      <script type="text/javascript">
      function validate_listbo x()
      {
      var x=document.getE lementById("cit y");
      if(x.options[x.selectedIndex]==0)
      {
      alert("Select a City");
      document.getEle mentById("city" ).focus();
      return false;
      }
      }
      </script>

      Comment

      • DrBunchman
        Recognized Expert Contributor
        • Jan 2008
        • 979

        #4
        When you call your Javascript do you use a return? e.g.

        Code:
        <input type="submit" value="Submit Form" OnClick="return validate_listbox()" />
        You should put an else in your function which returns true as well. Like this:

        Code:
        function validate_listbox() 
        {
        var x=document.getElementById("city");
        if(x.options[x.selectedIndex]==0)
        {
        alert("Select a City");
        document.getElementById("city").focus();
        return false;
        }
        else
        {
        return true;
        }
        }
        Does this help?

        Dr B

        Comment

        • Vini171285
          New Member
          • Apr 2008
          • 60

          #5
          actually i could give focus on the list box...but after clicking the alert button,it focuses on the list box,,,and before doing any change it immediately passes to the next page...
          This is my problem...

          Comment

          • DrBunchman
            Recognized Expert Contributor
            • Jan 2008
            • 979

            #6
            Did you try putting a 'return' in as I suggested in my previous post? If not please try it and if so please print the code that you are using now.

            Dr B

            Comment

            • Vini171285
              New Member
              • Apr 2008
              • 60

              #7
              Hi
              Thanx..
              Return statement is Working...
              Thank You...

              Comment

              • DrBunchman
                Recognized Expert Contributor
                • Jan 2008
                • 979

                #8
                No problem, glad to be of help.

                Dr B

                Comment

                Working...