Use image to select radio button in selectOneRadio elements

I am trying to create a user interface using selectOneRadio primers. Currently, I need to be able to display 5 images, which are actually radio buttons, and when I select an image, the image turns blue. The actual switch is locked.

I tried firstfaces selectOneRadio with a custom layout, but when I click on the image the radio button is not selected, and the button is also visible.

Here is my code. I use the base for css.

<ul class="small-block-grid-5">
            <p:selectOneRadio value="scooter" id="vehicleType" layout="custom">
                <f:selectItem itemValue="scooter" />
                <f:selectItem itemValue="bike"/>
                <f:selectItem itemValue="car"/>
                <f:selectItem itemValue="plane" />
                <f:selectItem itemValue="unknown" />
                <f:ajax event="click" render="addCost"/>
            </p:selectOneRadio>

            <li>
                <p:radioButton id="scooter" for="vehicleType" itemIndex="0"></p:radioButton>
                <h:graphicImage for="scooter" value="/images/scooter.GIF" width="50" height="50"/>
            </li>
            <li>
                <p:radioButton id="bike" for="vehicleType" itemIndex="1"></p:radioButton>
                <h:graphicImage for="bike" value="/images/bike.GIF" width="50" height="50"/>
            </li>
            <li>
                <p:radioButton id="car" for="vehicleType" itemIndex="2"></p:radioButton>
                <h:graphicImage for="car" value="/images/car.GIF" width="50" height="50"/>
                <h:panelGroup id="addCost">
                <h:outputText styleClass="breadcrumbs" value="Add 10$" />
                </h:panelGroup>
            </li>
            <li>
                <p:radioButton id="plane" for="vehicleType" itemIndex="3"></p:radioButton>
                <h:graphicImage for="plane" value="/images/plane.GIF" width="50" height="50"/>
            </li>
            <li>
                <p:radioButton id="unknown" for="vehicleType" itemIndex="4"></p:radioButton>
                <h:graphicImage for="unknown" value="/images/unkown.GIF" width="50" height="50"/>
            </li>
        </ul>
0
source share
1 answer

I solved this using some css and framework.

             <ul class="small-block-grid-5">
                <p:selectOneRadio value="scooter" id="vehicleType" layout="custom">
                    <f:selectItem itemValue="scooter" />
                    <f:selectItem itemValue="bike"/>
                    <f:selectItem itemValue="car"/>
                    <f:selectItem itemValue="plane" />
                    <f:selectItem itemValue="unknown" />
                    <f:ajax event="click" render="addCost"/>
                </p:selectOneRadio>

                <li>
                <div class="row">
                    <div class="small-1 columns invisibleDiv ">
                    <p:radioButton id="scooter" for="vehicleType" itemIndex="0"></p:radioButton>
                    </div>
                    <div class="small-11 columns imgSec">
                    <h:graphicImage for="scooter" value="/images/scooter.GIF" width="50" height="50"/>
                    </div>
                </div>
                </li>
            </ul>

css

.invisibleDiv {
    visibility: hidden;
}
.borderDiv {
    border: 1px solid blue;
}

js

$(".imgSec").click(function(){
        var radioInput= $(this).prev(".invisibleDiv ");
        $(".imgSec").removeClass("borderDiv");
        $(this).addClass("borderDiv");
    });
0
source

Source: https://habr.com/ru/post/1761508/


All Articles