Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use <p:commandLink> just for JS execution without form submit

How can I make <p:commandLink> without submit? Just for JS execution. For <p:commandButton> I can do it this way:

<p:commandButton value="JS button" type="button" onclick="alert('clicked')"/>

What is the analog for <p:commandLink>?

like image 667
Gregory Avatar asked Jun 29 '15 07:06

Gregory


1 Answers

Just return false from the JavaScript handler.

<p:commandLink ... onclick="alert('clicked'); return false;" />

The same applies to all other on* attributes and the command button, by the way.

<p:commandButton ... onclick="alert('clicked'); return false;" />

Whenever you return false from any JavaScript on* attribute on a HTML element, the element's default action/behavior (in this case, submitting the form) will be blocked.

Nonetheless, if you don't need to invoke a JSF backing bean action method after all, just don't use a command link/button component in first place. Use a plain link/button component.

<h:link ... onclick="alert('clicked'); return false;" />
<p:button ... onclick="alert('clicked'); return false;" />

Those doesn't need to be placed in a form, too.

like image 93
BalusC Avatar answered Nov 09 '22 08:11

BalusC