Ketan Bhavsar
Ketan Bhavsar

Reputation: 5396

a4j:commandLink with onClick return true or false not properly working

My code is like below,

<a4j:commandLink  oncomplete="openSignatureModel();" id="requestText"  reRender="sigPassword"  title="Send Prescription"
actionListener="#{patientNotePresBackingBean.sendPrescriptionId}"
onclick="return pharmacyCheck('#{row}');"> 
<f:param name="sendPrescriptionId" value="#{patientNotePrescriptionVar.id}" />
<f:param name="processOfPrescription" value="Send" />
<h:graphicImage value="/images/send-prescribe.png" style="border:0;"></h:graphicImage>
</a4j:commandLink>

and my java script is like...

function pharmacyCheck(rowID) {
    var a = document.getElementById("prescriptionId:patientNotePresListId:"+rowID+":pharmacyRcopiaId");
    if(a.value == -1) {
        alert("Please select Pharmacy");
        return false;
    } else {
        return true;
    }

}

Actually when it returns true it should call patientNotePresBackingBean.sendPrescriptionId right?

But it's not calling the same.... Is there any reason why?

Upvotes: 3

Views: 7400

Answers (1)

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85779

When you see the generated HTML, something like this will be in the onclick method of the button/link:

onclick="return pharmacyCheck('#{row}');;A4J.AJAX.Submit(..."

So the onclick is returning at the beginning and the ajax submit will not be sent. To solve your problem, use your javascript method in this way:

<a4j:commandLink onclick="if (!pharmacyCheck('#{row}')) return false;" ...>

EDIT: Further explanation:

When you have a JavaScript code like this:

function X() {
    return false;
    alert("this alert won't be shown =(");
}

As obviously stated, the alert won't fire never. So, checking the generated HTML for onclick (specially formatted to see the similarities):

onclick="
    return functionX();
    alert('this alert will not display either X_x');
    A4J.AJAX.Submit(...
"

The onclick code won't fire because you're returning something before executing the code. The change will just intercept the false value and do a return, in case your method returns true, the rest of the code will be executed.

Note: By default, any javascript method that doesn't return anything, returns false.

Upvotes: 9

Related Questions