0

Below is my html code with Vue instance. I need to display the input fields on condition using a value that i will get from database. I have chosen to define the value manually to try it out

    <div id="codeTest">
        <input id="input1" type="text" />
        <input id="input2" type="text" />

        {{$data}}
    </div>
    <script type="text/javascript">
     var inputVM= new Vue({
         el:"#codeTest",
         data:function(){
             return{
                 value:1
             }
         },
         created:function(){
             this.showInput();
         },
         methods:{
             showInput:function(value){
                 if(value==1){
                     document.getElementById("input1").style.display = '';
                     document.getElementById("input2").style.display = 'none';
                 }
                 else if(value==2){
                     document.getElementById("input1").style.display = '';
                     document.getElementById("input2").style.display = '';
                 }
             }
         }
     })

   `I am getting two input field instead of one. How do i go about it? Thanks in advance` 


            //document.getElementById("test").style.display = 'none';


    </script>

1 Answer 1

1

Please reference for Vue v-if, v-show https://v2.vuejs.org/v2/guide/conditional.html

<div id="codeTest">
    <input v-show="value == 1" id="input1" type="text" />
    <input v-show="value == 2" id="input2" type="text" />

    {{$data}}
</div>

<script type="text/javascript">
 var inputVM= new Vue({
     el:"#codeTest",
     data: function() {
         return {
             value: 1
         }
     },
     created: function(){
         this.getValueFromDatabase();
     },
     methods: {
         getValueFromDatabase: function() {
            const valueData = callApiMethod(); // not defined
            this.value = valueData // 1 or 2
         }
     }
 })
 </script> 
Sign up to request clarification or add additional context in comments.

4 Comments

Good hack. Thanks
I have tried your style and it works. However, i need to display multiple fields for value more than 1. So that for value as 2 I display input1 and input 2. So for value 3, it shall be the first 2 and another extra one. How do i go about it?Thanks in advance
@Musa You can change condition on Vue If example: <input v-show="value == 2 || value == 3" id="input1" type="text" /> <input v-show="value == 2 || value == 3" id="input2" type="text" /> <input v-show="value == 3" id="input2" type="text" />
@1Rhino..ua concept looks awesome..lemi try it out.. Thanks

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.