This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Five Lines of Code and have Christian Clausen on-line!
See this thread for details.
Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

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

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Saloon Keeper
Posts: 22265
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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()".
 
Reginald Carter
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22265
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22265
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Quick! Before anybody notices! Cover it up with this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic