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. Referencing HTML input boxes correctly

Referencing HTML input boxes correctly

Scheduled Pinned Locked Moved ASP.NET
questionhtmlwinformshelp
6 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 Offline
    D Offline
    dptalt
    wrote on last edited by
    #1

    I have a user control with two input boxes. The onkeyup calls a function. The function works but the problem is when I have two of these user controls on a page. When document.getElementById("Text2").focus() executes for the second user control it puts the focus on Text2 in the first user control. How do I reference Text2 so that the focus is place on the correct Text2? function TimeEntryHoursOnClick(theTextbox) { if (event.keyCode == 38) theTextbox.value = parseFloat(theTextbox.value) + 1 if (event.keyCode == 39) document.getElementById("Text2").focus(); }

    N 1 Reply Last reply
    0
    • D dptalt

      I have a user control with two input boxes. The onkeyup calls a function. The function works but the problem is when I have two of these user controls on a page. When document.getElementById("Text2").focus() executes for the second user control it puts the focus on Text2 in the first user control. How do I reference Text2 so that the focus is place on the correct Text2? function TimeEntryHoursOnClick(theTextbox) { if (event.keyCode == 38) theTextbox.value = parseFloat(theTextbox.value) + 1 if (event.keyCode == 39) document.getElementById("Text2").focus(); }

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #2

      You need to use the ClientID to get the proper DOM element. Try looking here[^]


      only two letters away from being an asset

      D 1 Reply Last reply
      0
      • N Not Active

        You need to use the ClientID to get the proper DOM element. Try looking here[^]


        only two letters away from being an asset

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

        The code cannot find Text2.   This line returns null.   Any ideas why? document.getElementById("ctl00_ContentPlaceHolder1_TimeEntryBox1_Text2"); Here is the HTLM. <div id="ctl00_ContentPlaceHolder1_TimeEntryBox1_Panel1" style="border-color:#7F9DB9;border-width:1px;border-style:Solid;width:89px;"> <input id="Text1" name="Text1" onkeyup="TimeEntryHoursOnClick(this)" onblur="TimeEntryHoursOnBlur(this)" style="border: 0px none #FFFFFF; width: 12px; " type="text" maxlength="2" /> : <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>

        N A 2 Replies Last reply
        0
        • D dptalt

          The code cannot find Text2.   This line returns null.   Any ideas why? document.getElementById("ctl00_ContentPlaceHolder1_TimeEntryBox1_Text2"); Here is the HTLM. <div id="ctl00_ContentPlaceHolder1_TimeEntryBox1_Panel1" style="border-color:#7F9DB9;border-width:1px;border-style:Solid;width:89px;"> <input id="Text1" name="Text1" onkeyup="TimeEntryHoursOnClick(this)" onblur="TimeEntryHoursOnBlur(this)" style="border: 0px none #FFFFFF; width: 12px; " type="text" maxlength="2" /> : <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>

          N Offline
          N Offline
          Not Active
          wrote on last edited by
          #4

          Have you tried debugging the JavaScript?


          only two letters away from being an asset

          1 Reply Last reply
          0
          • D dptalt

            The code cannot find Text2.   This line returns null.   Any ideas why? document.getElementById("ctl00_ContentPlaceHolder1_TimeEntryBox1_Text2"); Here is the HTLM. <div id="ctl00_ContentPlaceHolder1_TimeEntryBox1_Panel1" style="border-color:#7F9DB9;border-width:1px;border-style:Solid;width:89px;"> <input id="Text1" name="Text1" onkeyup="TimeEntryHoursOnClick(this)" onblur="TimeEntryHoursOnBlur(this)" style="border: 0px none #FFFFFF; width: 12px; " type="text" maxlength="2" /> : <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>

            A Offline
            A Offline
            Abhishek Sur
            wrote on last edited by
            #5

            getElementById is a javascript function that finds one element. If the ID of the textbox is Text1 when rendered to the client browser (I mean if you see id of your input as Text1 when you see Source from the browser), just use var element = document.getElementById("Text1"); rather than var element = document.getElementById("ctl00_ContentPlaceHolder1_TimeEntryBox1_Text2"); If you want to find the div, you can do so using var divelement = document.getElementById("ctl00_ContentPlaceHolder1_TimeEntryBox1_Panel1"); Always remember, you can mention only those elements which are rendered to the client. ASP.NET server side Ids are different than what is rendered in the client side. Use ClientID than ID for any server control to get the actual ID generated in the client, and always call this ID through JAVASCRIPT. Hope you got it. :thumbsup:

            Abhishek Sur


            My Latest Articles **Create CLR objects in SQL Server 2005 C# Uncommon Keywords Read/Write Excel using OleDB

            **Don't forget to click "Good Answer" if you like to.

            D 1 Reply Last reply
            0
            • A Abhishek Sur

              getElementById is a javascript function that finds one element. If the ID of the textbox is Text1 when rendered to the client browser (I mean if you see id of your input as Text1 when you see Source from the browser), just use var element = document.getElementById("Text1"); rather than var element = document.getElementById("ctl00_ContentPlaceHolder1_TimeEntryBox1_Text2"); If you want to find the div, you can do so using var divelement = document.getElementById("ctl00_ContentPlaceHolder1_TimeEntryBox1_Panel1"); Always remember, you can mention only those elements which are rendered to the client. ASP.NET server side Ids are different than what is rendered in the client side. Use ClientID than ID for any server control to get the actual ID generated in the client, and always call this ID through JAVASCRIPT. Hope you got it. :thumbsup:

              Abhishek Sur


              My Latest Articles **Create CLR objects in SQL Server 2005 C# Uncommon Keywords Read/Write Excel using OleDB

              **Don't forget to click "Good Answer" if you like to.

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

              Ok thanks for the info, but how would 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>

              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