Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7042045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:07:13+00:00 2026-05-28T02:07:13+00:00

The onclick attribute of my commandButton has some EL dependent Javascript inside. To be

  • 0

The onclick attribute of my commandButton has some EL dependent Javascript inside. To be more specific, here is that piece of code:

<p:commandButton
    onclick="javascript: if('#{userBean.user.friendList.size() gt 0}' == 'true') deleteFriendsConfirmDialog.show(); else friendsAlreadyDeletedErrorDialog.show();"
    value="Delete all friends?" />

deleteFriendsConfirmDialog clears the list of friends and updates the @form. The list of friends, commandButton and the dialogs are all in that form.

So I click the button, confirmation dialog comes up (because the length of friends’ list is gt 0), I confirm and the list is emptied, the view is updated. However, when I click the Delete all friends? button once more, the confirmation dialog comes up again. Since the length of the list is now 0, I instead expect the error dialog to show.

That, I guess, is because the Javascript written inside onclick is not updated (although the button is in the form).

Edit: Changing #{userBean.user.friendList.size() gt 0} to #{not empty userBean.user.friendList} doesn’t work neither.

How come? What’s wrong here?

Any help appreciated. 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T02:07:14+00:00Added an answer on May 28, 2026 at 2:07 am

    Indeed. PrimeFaces (and standard JSF) does not re-evaluate the EL in on* attributes on a per-request basis. It only happens on a per-view basis. RichFaces however does that in <a4j:xxx> components.

    You need to solve the problem differently. I suggest to use the visible attribute of the <p:dialog> instead.

    <h:form>
        ...
        <p:commandButton value="Delete all friends?" update=":deleteDialogs" />
    </h:form>
    ...
    <h:panelGroup id="deleteDialogs">
        <p:dialog id="deleteFriendsConfirmDialog" visible="#{facesContext.postback and not empty userBean.user.friendList}">
            ...
        </p:dialog>
        <p:dialog id="friendsAlreadyDeletedErrorDialog" visible="#{facesContext.postback and empty userBean.user.friendList}">
            ...
        </p:dialog>
    </h:panelGroup>
    

    An alternative is to use PrimeFaces’ RequestContext in the bean’s action method which allows you to execute JavaScript code programmatically in bean’s action method (although this tight-couples the controller a bit too much with the view, IMO).

    <p:commandButton value="Delete all friends?" action="#{userBean.deleteAllFriends}" />
    

    with

    RequestContext context = RequestContext.getCurrentInstance();
    
    if (!user.getFriendList().isEmpty()) {
        context.execute("deleteFriendsConfirmDialog.show()");
    } else {
        context.execute("friendsAlreadyDeletedErrorDialog.show()");
    }
    

    Unrelated to the concrete problem, your original onclick, while it does not work out in your particular case, shows some poor practices. The javascript: pseudoprotocol is superfluous. It’s the default already. Remove it. Also the test against == 'true' is superfluous. Remove it. Just let the EL print true or false directly. The following is the proper syntax (again, this does not solve your problem, just for your information)

    <p:commandButton
        onclick="if (#{not empty userBean.user.friendList}) deleteFriendsConfirmDialog.show(); else friendsAlreadyDeletedErrorDialog.show();"
        value="Delete all friends?" />
    

    It would have worked if you were using RichFaces’ <a4j:commandButton>.

    <a4j:commandButton
        oncomplete="if (#{not empty userBean.user.friendList}) deleteFriendsConfirmDialog.show(); else friendsAlreadyDeletedErrorDialog.show();"
        value="Delete all friends?" />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some buttons with an onclick attribute and some that don't. I want
I have some HTML with an onclick attribute. I want to override that attribute
I have an internal website that has a link with NO onClick attribute present.
Using jquery, I'd like to get the javascript from an A tag's onClick attribute.
I have some code where a <script type=text/javascript> block is dynamically inserted. This block
I have a onclick event in JavaScript that I have chosen to format as
I have a link which has an onclick attribute, and there is a toggle
I'am trying to change onclick attribute in jQuery but it doesn't change, here is
I'm having trouble trying to properly insert additional code into the onclick attribute of
I have code <a href=javascript:someFunction(this); tabindex=0>Some text</a> Is it possible to pass element into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.