0

I am building a radio button, which use a default image (black-radio.png or white-radio.png without checkmark) when the button is not checked, else they use black-radio-checked.png or white-radio-checked.png. But really stuck with this bug. I know there is an option to use computed property, but how should I solve this?

enter link description here

enter image description here

var vm = new Vue({
    el: '#colorPicker',
    data: {
        color: 'black',
        colors: [
            {
                name: 'black',
                image: 'images/black-radio.png',
                image_checked: 'images/black-radio-checked.png'
            },
            {
                name: 'white',
                image: 'images/white-radio.png',
                image_checked: 'images/white-radio-checked.png'
            }
        ]
    }
});
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js" type="text/javascript"></script>

<div id="colorPicker">				
<div class="display-inline-block">
    <label :for="colorInfo.name" v-for="(colorInfo, index) in colors">
      <input type="radio" name="color" v-model="color" :id="colorInfo.name" class="wireless-headphone-new-input" :value="colorInfo.name"/>
      <img :src="'images/' + colorInfo.name + '-radio.png'">
    </label>
  <div class="confirmation-color capitalize">{{ color }}  </div>
</div>
</div>

1
  • What do you mean by conditional statement? do you want to show image based on selected checkbox? Commented Aug 24, 2018 at 15:51

1 Answer 1

1

Whenever a radio button is checked, this.color value is updated.
So a v-if check on color==colorInfo.name to show image_checked field for the selected radio button and v-else to show rest other buttons with image field will do the trick.

var vm = new Vue({
    el: '#colorPicker',
    data: {
        color: '',
        colors: [
            {
                name: 'black',
                image: 'images/black-radio.png',
                image_checked: 'images/black-radio-checked.png'
            },
            {
                name: 'white',
                image: 'images/white-radio.png',
                image_checked: 'images/white-radio-checked.png'
            }
        ]
    }
    
});
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js" type="text/javascript"></script>

<div id="colorPicker">				
<div class="display-inline-block">
    <label :for="colorInfo.name" v-for="(colorInfo, index) in colors">
      <input type="radio" name="color" v-model="color" :id="colorInfo.name" class="wireless-headphone-new-input" :value="colorInfo.name"/>
      <img v-if="color==colorInfo.name" :src="colorInfo.image_checked">
      <img v-else :src="colorInfo.image"/>
    </label>
  <div class="confirmation-color capitalize">{{ color }}  </div>
  
</div>
</div>

Sign up to request clarification or add additional context in comments.

12 Comments

Thanks, but how do I add interpolation to img src in vue? and there should be an if else statement or something like that....to check which image they should show (because you have a white-radio.png and white-radio-checked.png)
what interpolation do you want to add
You should look at this part--> <img :src="'images/' + colorInfo.name + '-radio.png'"> . I think it is not possible to add interpolation to an img src attribute like this--> <img :src="{{curentImgUrl}}/>....but this should also first check if the input is checked, if not then use black-radio.png else black-radio-checked.png.
it is possible, you can do <img :src=currentImgUrl />
Ok and how do i check if an input is checked or not....if checked -> black-radio.png else black-radio-checked.png...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.