Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I execute Javascript before a JSF <h:commandLink> action is performed? [duplicate]

Tags:

javascript

jsf

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?

like image 296
Grant Wagner Avatar asked Sep 16 '08 15:09

Grant Wagner


3 Answers

Can be simplified like this

onclick="return confirm('Are you sure?');"
like image 82
iordan Avatar answered Oct 28 '22 14:10

iordan


<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.

like image 45
Grant Wagner Avatar answered Oct 28 '22 12:10

Grant Wagner


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}"/>
like image 7
Hanynowsky Avatar answered Oct 28 '22 14:10

Hanynowsky