How do you invoke a bean method from Javascript in PrimeFaces?
I have the following menuItem:
<p:menuitem id="toggleAlarms" icon="ui-icon-cancel" rendered="#{navigationBean.admin}" value="Cancel Alert" update=":alarmMessages" action="#{alarmsBean.toggleAlertOff()}"/>
When I click on that item in the UI the method gets called.
But when I have this element:
<p:messages id="alarmMessages">
<script>
jQuery('#alarmMessages').effect("pulsate", {times:5}, 1000 );
jQuery('#alarmMessages').show();
$('#alarmMessages').click(function() {
jQuery('#toggleAlarms').click();
alert('fool');
})
</script>
</p:messages>
If I click on the messages object on the UI, I can see the alert message with fool in it.
However, I never see the toggleAlarms.click() method invoke the alarmsBean.toggleAlertOff() call.
Am I doing something wrong?
any chance that
#{navigationBean.admin}returnsfalse?cause if so you wont be able to find it in the DOM… (maybe better use
style=display:none)also , your selector might be wrong if the menuitem is inside form try
jQuery('#someFormID\\:toggleAlarms').click();do view source on your web page to see the right id of the menuitemfinally try the same with commandButton instead (not sure that menuitem will respond as expected to
.click())Update
in order to check if your jquery selector is right use
alert($('#toggleAlarms').length)instead ofalert('fool');