Search code examples
cssjsfonclickmanaged-bean

JSF change css style attribute onclick


To be clear, I have a div with a certain style and when I click on a commandButton inside this div I want to set the display attribute to "none".

I want that the css style change by clicking on the commandButton.

So here is my xhtml :

<div class="details" id="detailsPopUp" style="#{detailsPopUpManager.style}">
                <h:commandButton image="resources/images/close Icon.png" 
                                 action="#{detailsPopUpManager.closePopUp()}" 
                                 class="closeIcon"/>
</div>

And this is the detailsPopUpManager code :

 @Named(value = "detailsPopUpManager")

 @SessionScoped

 public class DetailsPopUpManager implements Serializable {

    private String style;
    public DetailsPopUpManager() {
        style = "width:500px;\n" +
                "height:500px;\n" +
                "background: graytext;\n" +
                "position: absolute;\n" +
                "right: 25%;\n" +
                "top: 10px;\n" +
                "z-index: 1;";
    }

    public String getStyle() {
        return style;
    }

    public void setStyle(String style) {
        this.style = style;
    } 

    public void closePopUp()
    {
        style = "width:500px;\n" +
                "height:500px;\n" +
                "background: graytext;\n" +
                "position: absolute;\n" +
                "right: 25%;\n" +
                "top: 10px;\n" +
                "z-index: 1;\n" +
                "display: none;";
    }

Thank's for further help.


Solution

  • You need to use ajax to achieve that result.

    <h:form id="myForm">
       <div class="details" id="detailsPopUp" style="#{detailsPopUpManager.style}">
                    <h:commandButton image="resources/images/close Icon.png" 
                                     action="#{detailsPopUpManager.closePopUp()}" 
                                     class="closeIcon">
                       <f:ajax render="myForm"/>
                    </h:commandButton>
    
       </div>
    </h:form>