If you have a JSF <h:commandLink>
(which uses the onclick
event of an <a>
to submit the current form), how do you execute JavaScript (such as asking for delete confirmation) prior to the action being performed?
Can be simplified like this
onclick="return confirm('Are you sure?');"
<h:commandLink id="myCommandLink" action="#{myPageCode.doDelete}">
<h:outputText value="#{msgs.deleteText}" />
</h:commandLink>
<script type="text/javascript">
if (document.getElementById) {
var commandLink = document.getElementById('<c:out value="${myPageCode.myCommandLinkClientId}" />');
if (commandLink && commandLink.onclick) {
var commandLinkOnclick = commandLink.onclick;
commandLink.onclick = function() {
var result = confirm('Do you really want to <c:out value="${msgs.deleteText}" />?');
if (result) {
return commandLinkOnclick();
}
return false;
}
}
}
</script>
Other Javascript actions (like validating form input etc) could be performed by replacing the call to confirm()
with a call to another function.
This worked for me:
<h:commandButton title="#{bundle.NewPatient}" action="#{identifController.prepareCreate}"
id="newibutton"
onclick="if(confirm('#{bundle.NewPatient}?'))return true; else return false;"
value="#{bundle.NewPatient}"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With