javascript - How do I bind a class if the input type attribute is checkbox? + Vuejs -


i have following line of code in .vue template input field component want check whether input field checkbox , bind class on input field if true.

in component looks this:

<input :type="type" class="o-pf-input" :class="!ischeckbox ? 'o-pf-input--cbx' : ''" :placeholder="placeholder" :name="placeholder" :value = 'value' @input="value = $event.target.value"> 

where has :class="!ischeckbox ? 'o-pf-input--cbx' : ''"

in data option have this:

data: function() {     return {         value: '',         checkbox: 'o-pf-input--cbx',         ischeckbox: false     } }, 

so kind of working applies class input fields don't want achieve. should add class when attribute type checkbox.

i guess type prop? move logic computed property , check if type checkbox:

computed: {   checkboxclass: function () {     return this.type === 'checkbox' ? 'o-pf-input--cbx' : ''   } } 

which used:

<input :type="type" class="o-pf-input" :class="checkboxclass" :placeholder="placeholder" :name="placeholder" :value = 'value' @input="value = $event.target.value"> 

you can read more computed properties here: https://vuejs.org/v2/guide/computed.html

if type isn't known might have reference actual element , check type manually. see here reference on ref: https://vuejs.org/v2/api/#ref


Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -