0

I want to get p:selectBooleanCheckbox component and check if it is selected or not using javascript

<h:dataTable value="#{controller.list}" var="item">
    <h:column>
        <f:facet name="header">Ratio1</f:facet>
        <h:panelGrid columns="2">
            <p:inputText id="ratio1" readonly="#{true}" disabled="true" styleClass="ratio1"/>
            <h:outputText value="%" />
        </h:panelGrid>
        <p:selectBooleanCheckbox id="report1"  onchange="calculateTotalRatio()" value="#{controller.value}" valueChangeListener="#{fISHController.onCaseTestItemPatternReportFlagChange()}">    
        </p:selectBooleanCheckbox>          
    </h:column>

I want on calculateTotalRatio() function check if the checkbox is checked or not and depending on it update ratio1 input text with value

Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
Raied Raafat
  • 93
  • 1
  • 3
  • 11
  • Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the [How to Ask](http://stackoverflow.com/help/how-to-ask) page for help clarifying this question. – Zakaria Acharki Jul 21 '16 at 14:29
  • Look this answer http://stackoverflow.com/a/11441530/6065134 – Artem Jul 21 '16 at 15:06
  • @ZakariaAcharki i added sample of my code, sorry i still beginner in stackoverflow – Raied Raafat Jul 27 '16 at 08:33

1 Answers1

4

You can define the widgetVar of your selectBooleanCheckbox

<p:selectBooleanCheckbox id="check" widgetVar="myCheckbox" value="#{myBacking.value}"/>

You can access in js the Primefaces object in these ways

directly with myCheckbox
PF("myCheckbox")
window["myCheckbox"]
PrimeFaces.widgets["myCheckbox"]

So, to get the checkbox state you can use

myCheckbox.input.is(':checked')
PF("myCheckbox").input.is(':checked')
window["myCheckbox"].input.is(':checked')
PrimeFaces.widgets["myCheckbox"].input.is(':checked')
SiMag
  • 586
  • 2
  • 8
  • @Thanks, but it not working correct if it is in datatable – Raied Raafat Jul 26 '16 at 15:51
  • @RaiedRaafat: Then via varStatus, add the index to the widgetVar name. Should work. And `window["myCheckbox"]` most likely only works in older PF versions or with compatibility mode turned on (if I am right). Since if I remember correctly, you can then also directly access it as `myCheckbox.input.is...` – Kukeltje Jul 28 '16 at 16:30
  • @Kukeltje are you sure that h:dataTable has a varStatus? p:dataTable has a rowIndexVar which can be used,for a rowIndex in h:dataTable [BalusC suggests to bind the dt with a backingbean](http://stackoverflow.com/a/10300771/6546865). – SiMag Jul 28 '16 at 17:15
  • Nope, sorry, I was wrong... I indeed meant rowIndexVar, but even that is only available in PrimeFaces. But since the OP is using PF anyway... ;-) – Kukeltje Jul 28 '16 at 18:09
  • @ Kukeltje @ BalusC , thanks very much it working now :) – Raied Raafat Aug 02 '16 at 09:33
  • Thanks! - Not sure if it's only version 8.0 that needs this, but I had to do this: PF("myCheckbox").jq.is(':checked') – Mike Oct 16 '20 at 21:31