VueJS conditionally add an attribute for an element

Javascriptvue.jsVuejs2

Javascript Problem Overview


In VueJS we can add or remove a DOM element using v-if:

<button v-if="isRequired">Important Button</button>

but is there a way to add / remove attributes of a dom element eg for the following conditionally set the required attribute:

Username: <input type="text" name="username" required>

by something similar to:

Username: <input type="text" name="username" v-if="name.required" required>

Any ideas?

Javascript Solutions


Solution 1 - Javascript

Try:

<input :required="test ? true : false">

Update: It has changed in Vue 3, see this answer https://stackoverflow.com/a/64598898

Solution 2 - Javascript

Simplest form:

<input :required="test">   // if true
<input :required="!test">  // if false
<input :required="!!test"> // test ? true : false

Solution 3 - Javascript

Conditional rendering of attributes changed in Vue 3. To omit an attribute use null or undefined.

Vue 2:

<div :attr="false">
Result: <div>

<div :attr="null">
Result: <div>

Vue 3:

<div :attr="false">
Result: <div attr="false">

<div :attr="null">
Result: <div>

Solution 4 - Javascript

<input :required="condition">

You don't need <input :required="test ? true : false"> because if test is truthy you'll already get the required attribute, and if test is falsy you won't get the attribute. The true : false part is redundant, much like this...

if (condition) {
    return true;
} else {
    return false;
}
// or this...
return condition ? true : false;
// can *always* be replaced by...
return (condition); // parentheses generally not needed

The simplest way of doing this binding, then, is <input :required="condition">

Only if the test (or condition) can be misinterpreted would you need to do something else; in that case Syed's use of !! does the trick.
  <input :required="!!condition">

Solution 5 - Javascript

You can pass boolean by coercing it, put !! before the variable.

let isRequired = '' || null || undefined
<input :required="!!isRequired"> // it will coerce value to respective boolean 

But I would like to pull your attention for the following case where the receiving component has defined type for props. In that case, if isRequired has defined type to be string then passing boolean make it type check fails and you will get Vue warning. To fix that you may want to avoid passing that prop, so just put undefined fallback and the prop will not sent to component

let isValue = false
<any-component
  :my-prop="isValue ? 'Hey I am when the value exist' : undefined"
/>

Explanation

I have been through the same problem, and tried above solutions !! Yes, I don't see the prop but that actually does not fulfils what required here.

My problem -

let isValid = false
<any-component
  :my-prop="isValue ? 'Hey I am when the value exist': false"
/>

In the above case, what I expected is not having my-prop get passed to the child component - <any-conponent/> I don't see the prop in DOM but In my <any-component/> component, an error pops out of prop type check failure. As in the child component, I am expecting my-prop to be a String but it is boolean.

myProp : {
 type: String,
 required: false,
 default: ''
}

Which means that child component did receive the prop even if it is false. Tweak here is to let the child component to take the default-value and also skip the check. Passed undefined works though!

<any-component
  :my-prop="isValue ? 'Hey I am when the value exist' : undefined"
/>
 

This works and my child prop is having the default value.

Solution 6 - Javascript

It's notable to understand that if you'd like to conditionally add attributes you can also add a dynamic declaration:

<input v-bind="attrs" />

where attrs is declared as an object:

data() {
    return {
        attrs: {
            required: true,
            type: "text"
        }
    }
}

Which will result in:

<input required type="text"/>

Ideal in cases with multiple attributes.

Solution 7 - Javascript

You can add colon before attribute (also can use conditions) like

<div :class="current? 'active': '' " > 
<button :disabled="InvalidForm? true : false " >

If you want to set a dynamic value like props then you also can use colon before attribute name like :

<Child :data="userList" />

Solution 8 - Javascript

In html use

<input :required="condition" />

And define in data property like

data () {
   return {
      condition: false
   }
}

Solution 9 - Javascript

You can use computed too

<input :required="isRequired" />

Computed:

computed: {
   isRequired () {
      return someLogic //Boolean, true or false
   }

Solution 10 - Javascript

You could write something like this:

<input type="text" name="username" :required="condition ? true : false">

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDon SmytheView Question on Stackoverflow
Solution 1 - JavascriptcymruuView Answer on Stackoverflow
Solution 2 - JavascriptSyedView Answer on Stackoverflow
Solution 3 - JavascriptJouni KantolaView Answer on Stackoverflow
Solution 4 - JavascriptStephen PView Answer on Stackoverflow
Solution 5 - JavascriptSatyam PathakView Answer on Stackoverflow
Solution 6 - Javascriptnikk wongView Answer on Stackoverflow
Solution 7 - JavascriptZaheer AlviView Answer on Stackoverflow
Solution 8 - JavascriptNipun JainView Answer on Stackoverflow
Solution 9 - JavascriptGuilherme NunesView Answer on Stackoverflow
Solution 10 - JavascriptVMK053View Answer on Stackoverflow