aspose file tools*
The moose likes JSF and the fly likes commandButton 'onclick' doesn't execute javascript method in XHTML page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "commandButton Watch "commandButton New topic
Author

commandButton 'onclick' doesn't execute javascript method in XHTML page

Reginald Carter
Greenhorn

Joined: Apr 14, 2008
Posts: 17
Hello,
I'm hoping someone can help me.
After a couple of weeks trying just about everything I'm wondering why I can't reach my java script code with the onclick event in commandButton.
Here is the XHTML page...




The java script accepts a big decimal value for Cost and Depreciation and attempts to insert the value (cost-depreciation) into the value field.
If anyone has any ideas I would be very grateful.

Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15665
    
  15

Hint: if you can remove the parts of samples that don't actually apply to your problem, it makes it easier for us to see what's wrong.

Pedantry: JSF backing beans are not Controllers. You don't write Controllers in JSF - they are pre-written parts of the JSF tag implementations. Except for the FacesServlet, which is the master controller, and is also something that you almost always use "as is". Backing beans are Models. The action code isn't Controller code, it's business logic independent of the MVC paradigm.

And finally, the answer. And you're going to kick yourself.

The reason the "onclick" won't work in your example is because your onclick attribute calls "setTheValue()". But the JavaScript you defined is named "setValue()".


Customer surveys are for companies who didn't pay proper attention to begin with.
Reginald Carter
Greenhorn

Joined: Apr 14, 2008
Posts: 17
Thank You for your reply Tim.
Actually the code I sent was wrong (from a text editor copy partially edited.)
Here is a slimmed down version of the code and it still doesn't work.

I've tried placing the javascript into the head as well and still no result.
I've also tried using a normal HTML head tag without the ui:define tag <ui:define name ="head">
If I use the following snippet:

The alert box does appear. but with my call to the javascript function it's dead Tim.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15665
    
  15

Well, it's not obvious why the javascript function isn't working (Q: have you tried making it ONLY call "alert"? without the other stuff?). However, the onclick attribute itself needs to return true or false, because onclick not only attaches functionality on the client side, it also checks to see if that functionality should suppress submitting the form to the server.
Reginald Carter
Greenhorn

Joined: Apr 14, 2008
Posts: 17
Hello Tim,
Yes
If I remove everything from the javascript function except the alert- The alert does show.
So at least we know that it's finding the function.
So there must be a problem with the call to getElementById.
I'm using Firefox. Is there something else that might work?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15665
    
  15

Do a "View Page Source". You'll see that the control whose JSF id is "deprecation" has a completely different ID at the HTML level, and the HTML ID is the one that JavaScript needs.

Also, put an id on your form, since the automatically-generated IDs ("j_xxx") can change without notice.

Incidentally, I think you mean "depreciation". "Deprecation" has a rather different meaning.
Reginald Carter
Greenhorn

Joined: Apr 14, 2008
Posts: 17
Hello Tim,
Thanks for all your help.
Your suggestion about giving the form an id and checking the 'Page Source' helped me resolve this issue.
When I make the call to getElementById using the form id with the element id it works like a charm.
Like this

Again Thank You!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: commandButton 'onclick' doesn't execute javascript method in XHTML page
 
Similar Threads
Need my rich:tree to be re loaded when I add a new entity to it
Ajax status doesn't stop in IE8
Component ID frm1_window viewid has already been found in the view
commandLink in table -> javax.servlet.ServletException: Can't find bundle
I need help with ajaxSingle, reRender and immediate.