Results 1 to 7 of 7

Thread: [JAVASCRIPT] Focus on Control Problem

  1. #1

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    [JAVASCRIPT] Focus on Control Problem

    Hi,

    I'm using this code:

    HTML Code:
    var e=$get('myTextBox'); 
    e.value = result; 
    e.focus();
    (yes, that's ajax mixed)

    This works ok in normal cases.

    But the focus on control works only once, the first time this code executes.

    When this code is executed again, I can see that the textbox value has changes (which means it is working), but the focus is not set on the textbox.

    Anyone.. any idea why?
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  2. #2

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: [JAVASCRIPT] Focus on Control Problem

    Interestingly if I add e.select() to it, it sets the focus on the control as well as selects the text.

    Code:
    var e=$get('myTextBox'); 
    e.value = result; 
    e.focus();
    e.select();
    But I don't want it selected. I only want the focus set on that textbox.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  3. #3

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: [JAVASCRIPT] Focus on Control Problem

    ok... I figured out a workaround by experimenting. If I call e.focus() twice, the focus is set there. Though I have no idea why.
    Code:
    var e=$get('myTextBox'); 
    e.value = result; 
    e.focus();
    e.focus();
    Anyways I'll leave the thread open for now for anyone who knows why this is happening, since what I'm doing is just a workaround and far from understanding.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  4. #4
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: [JAVASCRIPT] Focus on Control Problem

    Try:
    HTML Code:
     e.select = false;
    I haven't used ajax before so I'm not sure if that code is correct.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  5. #5

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: [JAVASCRIPT] Focus on Control Problem

    That's pure javascript. $get is just a shortcut function for document.GetElementById in ajax. Since I was dealing with partial postbacks, that function was available to me and I used it. And it is working fine too, since I can see the textbox being updated and any other javascript I may insert there; like alert(something) etc.

    The problem is why do I need to call the control.focus() method twice in quick succession to set the focus?? From what I know one call is the same as 100 calls, there should be nothing special going on there with the focus() method.

    BTW, I tried that e.select = false; but it doesn't work.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  6. #6
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: [JAVASCRIPT] Focus on Control Problem

    Maybe you are suppose to put enable/disable value inside the brackets?

    HTML Code:
    e.focus(True);
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  7. #7

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: [JAVASCRIPT] Focus on Control Problem

    Nope... that doesn't work either.

    e.focus() is a valid call and even works for the first time the function is executed. Problems happen from 2nd call onwards.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width