---
title: "Conditional html heading tag based on variable in knockout.js"  
description: "Conditional html heading tag based on variable in knockout.js"  
author: "Revati S Misra"  
published: 2024-06-26  
updated: 2024-06-27  
canonical: https://www.mindstick.com/forum/160797/conditional-html-heading-tag-based-on-variable-in-knockout-js  
category: "knockcout js"  
tags: ["javascript", "knockout.js", "knockout observables"]  
reading_time: 3 minutes  

---

# Conditional html heading tag based on variable in knockout.js

Conditional [html](https://www.mindstick.com/articles/1530/design-a-simple-stylish-calculator-using-html-css-and-javascript) [heading tag](https://www.mindstick.com/forum/156116/what-is-heading-tag) based on [variable](https://www.mindstick.com/articles/1807/objective-c-data-types-variables-object-creation) in knockout.js

## Replies

### Reply by Ashutosh Patel

#### Knockout.js Conditional Heading Tags

In Knockout.js, you can conditionally reference HTML elements such as [heading](https://yourviews.mindstick.com/view/83418/is-india-heading-towards-civil-war) tags (`<h1>`, `<h2>`, etc.) using data bindings based on variables. Here you can get a conditional representation of a heading tag based on a variable.

Suppose you have a visual model with an observable variable that determines the heading level to display (`headingLevel`). Here is a step-by-step example,

## Define Your ViewModel

```javascript
function AppViewModel() {
   this.headingLevel = ko.observable(1); // Default heading level is 1
   // You can change this value dynamically based on some condition
}
// Apply bindings
ko.applyBindings(new AppViewModel());
```

**HTML markup**\
Use Knockout.js data bindings to conditionally specify the header tag based on the `headingLevel` value.

```html
<div class="my-3">
    <!-- ko if: headingLevel() === '1' -->
    <h1>Heading Level 1</h1>
    <!-- /ko -->
    <!-- ko if: headingLevel() === '2' -->
    <h2>Heading Level 2</h2>
    <!-- /ko -->
    <!-- ko if: headingLevel() === '3' -->
    <h3>Heading Level 3</h3>
    <!-- /ko -->
    <!-- ko if: headingLevel() === '4' -->
    <h4>Heading Level 3</h4>
    <!-- /ko -->
    <!-- ko if: headingLevel() === '5' -->
    <h5>Heading Level 3</h5>
    <!-- /ko -->
</div>
```

- **Data Binding (**`ko if`**)** Knockout.js provides a conditional ko if binding to HTML based on the passed expression.
- **Observable (**`headingLevel`**)** `HeadingLevel` is an observable variable that you can update dynamically in your view model. When its value changes, Knockout.js automatically updates the corresponding HTML.

## Example-

Here is complete simple example that demonstrates how conditionally change the heading tags (`<h1>`, `<h2>`, `<h3>`, `<h4>`, and `<h5>`).

```html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" >
    <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.5.1/knockout-latest.min.js"></script>
    <title>Conditional heading tag Example</title>
</head>
<body>
    <div class="container my-3">
        <div id="headingExample">
            <p class="fw-semibold">Choose Heading Level:</p>
            <select data-bind="value: headingLevel"  class="form-select" aria-label="Default select example">
                <option value="1">Heading Level 1</option>
                <option value="2">Heading Level 2</option>
                <option value="3">Heading Level 3</option>
                <option value="4">Heading Level 4</option>
                <option value="5">Heading Level 5</option>
            </select>

                <div class="my-3">
                    <!-- ko if: headingLevel() === '1' -->
                    <h1>Heading Level 1</h1>
                    <!-- /ko -->
                    <!-- ko if: headingLevel() === '2' -->
                    <h2>Heading Level 2</h2>
                    <!-- /ko -->
                    <!-- ko if: headingLevel() === '3' -->
                    <h3>Heading Level 3</h3>
                    <!-- /ko -->
                    <!-- ko if: headingLevel() === '4' -->
                    <h4>Heading Level 3</h4>
                    <!-- /ko -->
                    <!-- ko if: headingLevel() === '5' -->
                    <h5>Heading Level 3</h5>
                    <!-- /ko -->
                </div>
        </div>
    </div>

    <script>
        function AppViewModel() {
            this.headingLevel = ko.observable('1'); // Default heading level is 1
        }

        // Apply bindings
        ko.applyBindings(new AppViewModel());
    </script>

</body>
</html>
```

## Output-

![Conditional html heading tag based on variable in knockout.js](https://www.mindstick.com/mindstickforums/006ef377-2173-4512-92bf-59b72abcd72f/images/6da2e406-b06d-40ad-a7a3-105693fe1e02.jpg)

Knockout.js allows you to conditionally display HTML elements such as heading tags based on visible variables. This approach uses Knockout’s data binding capabilities to keep your UI consistent with your underlying data model.

**Also, Read:** [How to integrate Knockout.js with other JavaScript libraries or frameworks?](https://www.mindstick.com/forum/160793/how-to-integrate-knockout-js-with-other-javascript-libraries-or-frameworks)


---

Original Source: https://www.mindstick.com/forum/160797/conditional-html-heading-tag-based-on-variable-in-knockout-js

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
