This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes Javascript Function Not Getting Called after setFocus() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Javascript Function Not Getting Called after setFocus()" Watch "Javascript Function Not Getting Called after setFocus()" New topic
Author

Javascript Function Not Getting Called after setFocus()

vikram solanki
Greenhorn

Joined: Dec 07, 2010
Posts: 12
Hi

I am using java-script in my application and i am in the middle of a bizarre situation.

I have used the below code in my page.


The problem that I am facing is after the setFocus() function is called the other function rendersection() is not called. But when i remove the setFocus() and then try, then the rendersection() is called. As seen in the above code i have also tried giving the rendersection() before the setFocus(). still it does not work. Does anyone know if i am missing anything here?

Also, when i tried to change the setFocus() to something different like putFocus or getFocus the function is called but the focus is not set to the desired field
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60766
    
  65

There are a lot of things to comment on here, but I'll just hit on a few:

First:
vikram solanki wrote:
renderSection('false');

If the function is expecting a boolean, why are you passing a string? Did you realize that if you use the string argument in a if clause that 'string" is truthy?

Be sure to be consistent with data typing. Use false not "false" for boolean values.

The most likely cause of failure is:
document.getElementById("{!$component.thePageBlock.state}").focus()

That's one bizarre id value and I suspect it's not real. This should cause an exception when the focus() method is attempted on an undefined reference.

Do you have the JavaScript console open so you can see any script errors?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1745
    
    2
Believe it or not, that id appears to be valid.

http://jsfiddle.net/QCwLd/2/
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60766
    
  65

Michael Matola wrote:Believe it or not, that id appears to be valid.

Oh, yes, it is technically legal (though insane) -- sorry for not being clear -- but to me it looks like server-side markup and so I'm not convinced that it's really the right id and the method is actually finding anything.
vikram solanki
Greenhorn

Joined: Dec 07, 2010
Posts: 12
sorry... I should have a bit more clearer. The js script i have shared here is used in a tool(or a platform) so the bizarre id. The id means that I am referring to an object(element) inside a page block with the id 'thePageBlock' and an element with the id 'state'. The setFocus() function works fine. It sets the focus to the correct element.

As for the error, I am not getting any in my console.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60766
    
  65

So have you 100% verified that the call to document.getElementById("{!$component.thePageBlock.state}") is returning an element?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Javascript Function Not Getting Called after setFocus()
 
Similar Threads
Refresh and put the view at the top of page
validation messages not displaying the error messages in the HTML boxes
window.onload in self invoking function
set focus
Struts2 Datetime Picker focus is not set using javascript