How to swap the displayed image based on which radio button is selected?

Goran Tesic

I have a bunch of column groups (col-md-4), each containing two absolutely positioned images on top of each other within a div. I need to switch the visible image within a group by changing its opacity from 0 to 1 based on which radio button is checked. Is there a way to do this with jquery?

<div class="row">
  <div class="col-md-4">
    <div class="slide-swapper">
      <img src="images/gray.jpg" class="img-fluid gray" height="360" width="360" alt="gray">
      <img src="images/silver.jpg" class="img-fluid silver" height="360" width="360" alt="silver">
    </div> 
    <div class="color-group">
      <input id="input-1" type="radio" checked="checked" name="change-image">
      <label for="input-1" title="gray">Gray</label>
      <input id="input-2" type="radio" name="change-image">
      <label for="input-2" title="silver">Silver</label>
    </div>
  </div>  
</div>
<style>
    .slide-swapper {
        position: relative;
        height: 360px;
        margin-bottom: 30px;
    }
    img {
        position: absolute;
        top: 0;
        left: 0;
    }
</style>
Mike

My suggestion:

img {
  position: absolute;
  top: 0;
  left: 0;
  -webkit-transition: opacity 1s ease-in-out;
  -moz-transition: opacity 1s ease-in-out;
  -ms-transition: opacity 1s ease-in-out;
  -o-transition: opacity 1s ease-in-out;
  transition: opacity 1s ease-in-out;
}

.color-group {
  padding-top: 160px;
}

#input-1:checked ~ .blue {
  opacity: 0;
}

#input-2:checked ~ .pink {
  opacity: 0;
}
<div class="color-group">
  <input id="input-1" type="radio" checked="checked" name="change-image">
  <label for="input-1" title="gray">Pink</label>
  <input id="input-2" type="radio" name="change-image">
  <label for="input-2" title="silver">Blue</label>
  <img class="pink" src="http://placehold.it/360x150/ffbbdd" height="150" width="360" alt="Pink">
  <img class="blue" src="http://placehold.it/360x150/9acef9" height="150" width="360" alt="Blue">
</div>

jQuery Solution (as requested):

$(document).ready(function() {
  $(".color-group input").change(function() {
  	$(this).parent().prev().children("img:nth-of-type(2)").fadeToggle("slow");
  });
});
    .slide-swapper {
        position: relative;
        height: 150px;
        margin-bottom: 30px;
    }
    img {
        position: absolute;
        top: 0;
        left: 0;
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row">
  <div class="col-md-4">
    <div class="slide-swapper">
      <img src="https://placehold.it/360x150/ffbbdd" class="img-fluid gray" height="150" width="360" alt="gray">
      <img src="https://placehold.it/360x150/9acef9" class="img-fluid silver" height="150" width="360" alt="silver">
    </div> 
    <div class="color-group">
      <input id="input-1" type="radio" checked="checked" name="change-image">
      <label for="input-1" title="gray">Gray</label>
      <input id="input-2" type="radio" name="change-image">
      <label for="input-2" title="silver">Silver</label>
    </div>
  </div>  
</div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to disable buttons based on which radio button is selected?

From Dev

How to know which radio button is selected in jquery?

From Dev

how to know which radio button id selected of which row?

From Java

How can I know which radio button is selected via jQuery?

From Dev

How to find out which radio button the user selected in Javascript?

From Dev

How to check which radio button is selected if they're all runat server?

From Dev

How to retrieve which radio button within a LI is selected

From Dev

how to know which radio button is selected out of all options?

From Dev

How to check which radio button is selected if they're all runat server?

From Dev

Change image if radio button is selected

From Dev

How do I check which radio button is selected when there are multiple radio buttons with the same name? (using jquery)

From Dev

Having some trouble getting which radio button is selected and issuing a command based on the selection

From Dev

How to style selected radio button

From Dev

Show a dropdown based on proper selected radio button

From Dev

Find the selected radio button value from the displayed group of questions

From Dev

Select text based on which radio button is checked

From Dev

Ng-repeat hiding radio button based on Radio answer selected

From Dev

how to change background image dynamically only when radio button is selected and by pressing submit button

From Dev

How can I tell which radio button is selected with node.js

From Dev

How to get list of values which are selected in radio button after click on submit in Angular js

From Dev

Image not displayed based on single click of button

From Dev

How to check if radio buttons are selected on button click?

From Java

How to get value of selected radio button?

From Dev

How to check if the radio button is selected or not in Selenium WebDriver?

From Dev

How to get value of selected radio button in angularjs

From Dev

How To get Selected Radio Button Value in Mvc

From Dev

How to display a radio button selected using a variable?

From Dev

How to get the value of the selected radio button?

From Dev

How to get selected radio button from ToggleGroup

Related Related

  1. 1

    How to disable buttons based on which radio button is selected?

  2. 2

    How to know which radio button is selected in jquery?

  3. 3

    how to know which radio button id selected of which row?

  4. 4

    How can I know which radio button is selected via jQuery?

  5. 5

    How to find out which radio button the user selected in Javascript?

  6. 6

    How to check which radio button is selected if they're all runat server?

  7. 7

    How to retrieve which radio button within a LI is selected

  8. 8

    how to know which radio button is selected out of all options?

  9. 9

    How to check which radio button is selected if they're all runat server?

  10. 10

    Change image if radio button is selected

  11. 11

    How do I check which radio button is selected when there are multiple radio buttons with the same name? (using jquery)

  12. 12

    Having some trouble getting which radio button is selected and issuing a command based on the selection

  13. 13

    How to style selected radio button

  14. 14

    Show a dropdown based on proper selected radio button

  15. 15

    Find the selected radio button value from the displayed group of questions

  16. 16

    Select text based on which radio button is checked

  17. 17

    Ng-repeat hiding radio button based on Radio answer selected

  18. 18

    how to change background image dynamically only when radio button is selected and by pressing submit button

  19. 19

    How can I tell which radio button is selected with node.js

  20. 20

    How to get list of values which are selected in radio button after click on submit in Angular js

  21. 21

    Image not displayed based on single click of button

  22. 22

    How to check if radio buttons are selected on button click?

  23. 23

    How to get value of selected radio button?

  24. 24

    How to check if the radio button is selected or not in Selenium WebDriver?

  25. 25

    How to get value of selected radio button in angularjs

  26. 26

    How To get Selected Radio Button Value in Mvc

  27. 27

    How to display a radio button selected using a variable?

  28. 28

    How to get the value of the selected radio button?

  29. 29

    How to get selected radio button from ToggleGroup

HotTag

Archive