Friday, September 5, 2025
HomeLanguagesJavascriptv-else-if Directive in Vue.js

v-else-if Directive in Vue.js

v-else-if directive is a Vue.js directive used to toggle the display CSS property of an element depending on a condition when the if condition is not satisfied. First, we will create a div element with id as app and let’s apply the v-else-if directive to an element with data. Now we will create this data by initializing a Vue instance with the data attribute containing the value.

Syntax:

v-else-if="data"

Parameters: This function accepts a single parameter which is data or a condition.

Example 1: This example uses Vue.js to show an element with v-else-if using arithmetic conditions.




<!DOCTYPE html>
<html>
  
<head>
    <title>
        VueJS | v-else-if directive
    </title>
  
    <!-- Load Vuejs -->
    <script src=
    </script>
</head>
  
<body>
    <div style="text-align: center;
            width: 600px;">
        <h1 style="color: green;">
            neveropen
        </h1>
        <b>
            VueJS | v-else-if directive
        </b>
    </div>
  
    <div id="canvas" style=
        "border:1px solid #000000;
        width: 600px;height: 200px;">
  
        <div id="app">
            <h2 v-if="data > 50">
                data is greater than 50
            </h2>
            <h2 v-else-if="data < 50">
                data is smaller than 50
            </h2>
            <h2 v-else>
                data is equal to 50
            </h2>
        </div>
    </div>
  
    <script>
        var app = new Vue({
            el: '#app',
            data: {
                data: 40
            }
        })
    </script>
</body>
  
</html>            


Output:

Example 2: This example uses Vue.js to show an element with v-else-if using Booleans.




<!DOCTYPE html>
<html>
  
<head>
    <title>
        VueJS | v-else-if directive
    </title>
  
    <!-- Load Vuejs -->
    <script src=
    </script>
</head>
  
<body>
    <div style="text-align: center;
        width: 600px;">
          
        <h1 style="color: green;">
            neveropen
        </h1>
        <b>
            VueJS | v-else-if directive
        </b>
    </div>
  
    <div id="canvas" style=
        "border:1px solid #000000;
        width: 600px;height: 200px;">
  
        <div id="app">
            <h2 v-if="data">
                if is executed
            </h2>
            <h2 v-else-if="!data">
                else-if is executed
            </h2>
        </div>
    </div>
  
    <script>
        var app = new Vue({
            el: '#app',
            data: {
                data: false
            }
        })
    </script>
</body>
  
</html>                   


Output:

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32269 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6638 POSTS0 COMMENTS
Nicole Veronica
11802 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11866 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7027 POSTS0 COMMENTS
Thapelo Manthata
6704 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS