Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Setting focus with JavaScript

Setting focus with JavaScript

Scheduled Pinned Locked Moved ASP.NET
questionjavascriptarchitecture
11 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D dptalt

    With Javascript how do I set the focus to Text2 in ctl00_ContentPlaceHolder1_TimeEntryBox2_Panel1? <div id="ctl00_ContentPlaceHolder1_TimeEntryBox1_Panel1" style="border-color:#7F9DB9;border-width:1px;border-style:Solid;width:89px;"> <input id="Text2" name="Text2" type="text" onkeyup="TimeEntryMinutesOnClick(this)" onblur="TimeEntryMinutesOnBlur(this)" style="border: 0px none #FFFFFF; width: 12px" maxlength="2" />                            <input type="hidden" id="ctrlPrefix" name="ctrlPrefix" value='ctl00_ContentPlaceHolder1_TimeEntryBox1_'> </div> <div id="ctl00_ContentPlaceHolder1_TimeEntryBox2_Panel1" style="border-color:#7F9DB9;border-width:1px;border-style:Solid;width:89px;"> <input id="Text2" name="Text2" type="text" onkeyup="TimeEntryMinutesOnClick(this)" onblur="TimeEntryMinutesOnBlur(this)" style="border: 0px none #FFFFFF; width: 12px" maxlength="2" />                            <input type="hidden" id="ctrlPrefix" name="ctrlPrefix" value='ctl00_ContentPlaceHolder1_TimeEntryBox2_'> </div>

    J Offline
    J Offline
    Jay Royall
    wrote on last edited by
    #2

    I am the voice of inexperience but I'm pretty sure it's bad practice to have more than 1 control on the page with the same ID. Can't you give each control a unique ID and use Javascript's .Focus method?

    D 1 Reply Last reply
    0
    • D dptalt

      With Javascript how do I set the focus to Text2 in ctl00_ContentPlaceHolder1_TimeEntryBox2_Panel1? <div id="ctl00_ContentPlaceHolder1_TimeEntryBox1_Panel1" style="border-color:#7F9DB9;border-width:1px;border-style:Solid;width:89px;"> <input id="Text2" name="Text2" type="text" onkeyup="TimeEntryMinutesOnClick(this)" onblur="TimeEntryMinutesOnBlur(this)" style="border: 0px none #FFFFFF; width: 12px" maxlength="2" />                            <input type="hidden" id="ctrlPrefix" name="ctrlPrefix" value='ctl00_ContentPlaceHolder1_TimeEntryBox1_'> </div> <div id="ctl00_ContentPlaceHolder1_TimeEntryBox2_Panel1" style="border-color:#7F9DB9;border-width:1px;border-style:Solid;width:89px;"> <input id="Text2" name="Text2" type="text" onkeyup="TimeEntryMinutesOnClick(this)" onblur="TimeEntryMinutesOnBlur(this)" style="border: 0px none #FFFFFF; width: 12px" maxlength="2" />                            <input type="hidden" id="ctrlPrefix" name="ctrlPrefix" value='ctl00_ContentPlaceHolder1_TimeEntryBox2_'> </div>

      A Offline
      A Offline
      Abhijit Jana
      wrote on last edited by
      #3

      Just find the ID of the control using GetElementByID method and use ControlObject.Focus() method to set the foucs.

      Abhijit Jana | Codeproject MVP Web Site : abhijitjana.net Don't forget to click "Good Answer" on the post(s) that helped you.

      J D 2 Replies Last reply
      0
      • A Abhijit Jana

        Just find the ID of the control using GetElementByID method and use ControlObject.Focus() method to set the foucs.

        Abhijit Jana | Codeproject MVP Web Site : abhijitjana.net Don't forget to click "Good Answer" on the post(s) that helped you.

        J Offline
        J Offline
        Jay Royall
        wrote on last edited by
        #4

        But he has 2 controls with the same ID. Will this approach still work?

        A 1 Reply Last reply
        0
        • J Jay Royall

          But he has 2 controls with the same ID. Will this approach still work?

          A Offline
          A Offline
          Abhijit Jana
          wrote on last edited by
          #5

          Liqz wrote:

          But he has 2 controls with the same ID.

          Why ? It should not be. Or you can first run the application, from view Source what exact ID is generating for that page control then use Control.Focus(). This will work. But this is not a good approch.

          Abhijit Jana | Codeproject MVP Web Site : abhijitjana.net Don't forget to click "Good Answer" on the post(s) that helped you.

          J 1 Reply Last reply
          0
          • J Jay Royall

            I am the voice of inexperience but I'm pretty sure it's bad practice to have more than 1 control on the page with the same ID. Can't you give each control a unique ID and use Javascript's .Focus method?

            D Offline
            D Offline
            dptalt
            wrote on last edited by
            #6

            The HTML is coming from a User Control. Two of the same User Control are on the page. When viewing the source from the browser this is what it looks like.

            A 1 Reply Last reply
            0
            • A Abhijit Jana

              Liqz wrote:

              But he has 2 controls with the same ID.

              Why ? It should not be. Or you can first run the application, from view Source what exact ID is generating for that page control then use Control.Focus(). This will work. But this is not a good approch.

              Abhijit Jana | Codeproject MVP Web Site : abhijitjana.net Don't forget to click "Good Answer" on the post(s) that helped you.

              J Offline
              J Offline
              Jay Royall
              wrote on last edited by
              #7

              In his markup he has 2 HTML input controls both with the ID of "Text2". I was under the impression that this was not good. If he calls 'Text2.Focus' from his Javascript, which input control will get the focus?

              A 1 Reply Last reply
              0
              • D dptalt

                The HTML is coming from a User Control. Two of the same User Control are on the page. When viewing the source from the browser this is what it looks like.

                A Offline
                A Offline
                Abhijit Jana
                wrote on last edited by
                #8

                If you have the source code you can chaneg the ID with meaning full name or use the ID that you are getting from ViewSource for focusing.

                Abhijit Jana | Codeproject MVP Web Site : abhijitjana.net Don't forget to click "Good Answer" on the post(s) that helped you.

                D 1 Reply Last reply
                0
                • A Abhijit Jana

                  Just find the ID of the control using GetElementByID method and use ControlObject.Focus() method to set the foucs.

                  Abhijit Jana | Codeproject MVP Web Site : abhijitjana.net Don't forget to click "Good Answer" on the post(s) that helped you.

                  D Offline
                  D Offline
                  dptalt
                  wrote on last edited by
                  #9

                  When I use GetElementById("Text2") it always gets the one in within ctl00_ContentPlaceHolder1_TimeEntryBox1_Panel1. How do I get the one that is within ctl00_ContentPlaceHolder1_TimeEntryBox2_Panel1?

                  1 Reply Last reply
                  0
                  • J Jay Royall

                    In his markup he has 2 HTML input controls both with the ID of "Text2". I was under the impression that this was not good. If he calls 'Text2.Focus' from his Javascript, which input control will get the focus?

                    A Offline
                    A Offline
                    Abhijit Jana
                    wrote on last edited by
                    #10

                    I guess it would the first one. :) but he need to find out the id using GetElementbyID, Text2.Focus(), will not work.

                    Abhijit Jana | Codeproject MVP Web Site : abhijitjana.net Don't forget to click "Good Answer" on the post(s) that helped you.

                    1 Reply Last reply
                    0
                    • A Abhijit Jana

                      If you have the source code you can chaneg the ID with meaning full name or use the ID that you are getting from ViewSource for focusing.

                      Abhijit Jana | Codeproject MVP Web Site : abhijitjana.net Don't forget to click "Good Answer" on the post(s) that helped you.

                      D Offline
                      D Offline
                      dptalt
                      wrote on last edited by
                      #11

                      How?

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups