views:

2121

answers:

4

I am using a command button from JSF. I don't know why I can't call my javascript function. NO alert will show when I click the button.

<h:commandButton id="login" value="Login" action="login"
   onclick="return checkPasswords();" type="Submit" />

My Javascript function:

function checkPasswords() {
    alert("test");
    return false;
}
+1  A: 

This is working

<script type="text/javascript">
function checkPasswords() {
   alert("test");
   return false;
}

<h:commandButton  id="login" value="Login" action="login"
             onclick="checkPasswords();" type="submit"/>
javaloper
+2  A: 

Give s in lowercase in type="submit", The type attribute sets the type of button to create for this component. The valid values for this attribute are "submit" and "reset". The default value for this attribute is "submit".

valli
This can impossibly be the cause of the problem. You already said yourself, the default is "submit". If "Submit" were wrong for some reasons, then "submit" would have been used. I am surprised about the 2 upvotes you fairly quickly got for this wrong answer. That would mean that those upvoters itself know nothing about JSF and assumed everything for right.
BalusC
Default is "submit" and you don't need to give it, if type is given in tag it will check for value "submit" or "reset" and if it is not equal what it expects it will not submit.
valli
Thus, you didn't test it at all or you're using an old JSF version which has a bug.
BalusC
+1  A: 
  1. Check your generated code (open the page -> view source)
  2. Check your javascript console (Firefox) for errors
  3. make sure your function is callable from a normal <input type="button">
  4. lowercase your button type.

Otherwise, it should work - I have exactly the same piece of code in my current codebase that works perfectly.

Bozho
A: 

Check the generated HTML source. Is the onclick there? If so, can you post an extract with all relevant/related code? If not (even more, most of HTML attributes are missing), then you've a collision in the JSF libraries used.

Since JSF Mojarra 1.2_05 there was a performance enhancement in generating of the standard HTML attributes and the logic was also moved from jsf-impl.jar to the jsf-api.jar. If you have a JSF API of older than 1.2_05 wandering in the classpath while you have a JSF impl of 1.2_05 or newer in your webapp, then you will see this problem. Most of the HTML attributes except of id, name, class such as onclick, onmouseover, etcetera are missing in generated HTML source.

You see this problem often when you've different versioned JSF libraries in both the Appserver/lib and the Webapp/WEB-INF/lib and then often in Glassfish servers. In case of loose JSF libraries in Appserver/lib, just remove them. They belongs in Webapp/WEB-INF/lib. In case of Glassfish which has the JSF libraries merged into its javaee.jar file, you should follow the Glassfish upgrade instructions along the JSF download.

BalusC