How to create a Radio button with image?

Asked

Viewed 5,204 times

5

How can I make a Radio Button turn into an Image? I need to make an image gallery inside a form to, when clicking on the image would be selecting a radio button as in the image below:

exemplo

  • You can only select one image at a time like a radio button or you want to be able to select several at a time?

  • Ola Sergio... yes one at a time

1 answer

10


You don’t need jQuery or Javascript for this. Use label and CSS.

Each element input may be associated with a label which it transmits to the input clicks. If you hide the input you get the functionality you want. Note: to hide the input you have to use visibility: hidden; or opacity: 0; because you hide with display: none; it will not be sent via form.

So having input and label side by side (and it has to be that way for CSS logic to work) you can have something like this:

input[type="radio"] {
        visibility: hidden;
}
    
label {
    display: block;
    border: 4px solid #666;
    width: 60px;
    float: left;
}

input[type="radio"]:checked+label {
    border-color: #ccf;
}
img {
  height: 50px;
}
<input type="radio" name="imagem" id="i1" />
<label for="i1"><img src="http://vkontakte.ru/images/gifts/256/81.jpg" alt=""></label>
<input type="radio" name="imagem" id="i2" />
<label for="i2"><img src="http://vkontakte.ru/images/gifts/256/82.jpg" alt=""></label>
<input type="radio" name="imagem" id="i3" />
<label for="i3"><img src="http://vkontakte.ru/images/gifts/256/83.jpg" alt=""></label>

jsFiddle: https://jsfiddle.net/0u1qqx6c/

  • Perfect was just that, only I have a little problem... the page has other Abels and this getting the css of the radio bottons

  • 1

    @Fabiohenrique this is simple to solve. Give these elements a specific class to distinguish them in the CSS or create a div for them like this: https://jsfiddle.net/0u1qx6c/2/

  • Sergio this script would have some other way to work without using id= from the input because it conflicted with a js that also picks up id=

  • @Fabiohenrique my solution has no script, and that is the best. Can you explain better where it gives conflict? You can also do it with Javascript, but I’d rather help you do it with HTML.

  • to place here the script https://jsfiddle.net/fabioo7/by1nvtdh/

  • @Fabiohenrique what is giving problem is this line -> params += '&foto='+document.getElementById('foto[]').value;?

  • yes... And this line even like this js also takes the id ai to work radio css,

  • 1

    @Fabiohenrique in this case you can use another selector. Interestingly this input should have attribute value right? Forehead like this: params += '&foto='+document.querySelector('input[type="radio"]:checked').value;. It would be nice to have a name these fields... In this case it would be even simpler! If you show the whole HTML (rendered) I can help you simplify this Javascript.

  • 1

    Show worked... thanks for the help

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.