Setting focus with JavaScript
-
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>
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?
-
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>
Just find the ID of the control using
GetElementByID
method and useControlObject.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.
-
Just find the ID of the control using
GetElementByID
method and useControlObject.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.
But he has 2 controls with the same ID. Will this approach still work?
-
But he has 2 controls with the same ID. Will this approach still work?
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.
-
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?
-
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.
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?
-
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.
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.
-
Just find the ID of the control using
GetElementByID
method and useControlObject.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.
-
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?
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.
-
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.