user3263117
user3263117

Reputation: 477

submit based on the Javascript function called via onclick attribute in <h:commandbutton>

In a form i used i called a Javascript function. Though the function returns false , form is submitted and action method is called.

i used like this

<h:commandButton onclick="test();" action="{#bean.menthod}">

test- javascript method returns boolean value false.

Above doesnt work. Below mentioned code alone worked. I want to know why

 <h:commandButton onclick="if(test) return true;else return false" action="{#bean.menthod}">

Upvotes: 0

Views: 1943

Answers (3)

raji
raji

Reputation: 447

Thanks.. This helped me lot. Here simple logic which i missed i.e .. in that onclick function I am returning false instead of true, which is stopping the action to be called. Finally i changed return value to true, and solved my issue.

Upvotes: 0

Rami
Rami

Reputation: 328

It is not working simply because the result of test() function is not returned to the component.

it should work as expected with

<h:commandButton onclick="return test()" action="{#bean.menthod}">

and this is exactly the same as your second case but simplified.

Upvotes: 1

Kishor Prakash
Kishor Prakash

Reputation: 8151

At the end of the day your h:commandButton will become a HTML Form submit button. i.e, <input type="submit" .../>

Usually form won't get submitted when the submit button returns false.

Upvotes: 0

Related Questions