Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

418
Views
Show/Hide HTML Elements from a Input Select option in Blazor Server app

I am using the following Javascript function to show elements when a button is triggered.

function showDiv() {
document.getElementById('myDIV').style.display = "block"; }

I call this function on a button with

@onclick=showDiv

I would like to do the same but on an InputSelect List. I have tried this but it doesn't work

<label>Number of engines:</label>
<InputSelect style="width: 25%" @bind-Value="model.NumberofEngines" class="form-control">
                    <option value="">-- Select the number of engines --</option>
                    <option>1</option>
                    <option @onclick=ShowDiv>2</option>
                    <option value="3">3</option>
                    <option value="4">4</option>
</InputSelect>

The idea is a calculator that calculates ship emissions and the user should choose the number of engines the ship has and the app should show the required inputs.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

I think you should Use onchange Test this code:

function onChangeDiv(divId, element)
{
    document.getElementById(divId).style.display = element.value == 1 ? 'block' : 'none';
}
<select name="frm_select" onchange="onChangeDiv('result', this)">
   <option value="1">show</option>
   <option value="0">hidden</option>
</select>
<div id="result">This is a div</div>

about 4 years ago · Juan Pablo Isaza Report

0

You can use simple css style to show hide given div on value in model.NumberofEngines

<div style="display: @(model.NumberofEngines == "1"  ? "block" : "none");">
    ONE
</div>

Here is complete code

<EditForm>
    <label>Number of engines:</label>
    <InputSelect style="width: 25%" @bind-Value="model.NumberofEngines" class="form-control">
                    <option value="">-- Select the number of engines --</option>
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                    <option value="4">4</option>
    </InputSelect>
</EditForm>


<div style="display: @(model.NumberofEngines == "1"  ? "block" : "none");">
    ONE
</div>


<div style="display: @(model.NumberofEngines == "2"  ? "block" : "none");">
    TWO
</div>

<div style="display: @(model.NumberofEngines == "3"  ? "block" : "none");">
    THREE
</div>

<div style="display: @(model.NumberofEngines == "4"  ? "block" : "none");">
    FOUR
</div>

Alternatively you can use css class also to show hide given div

about 4 years ago · Juan Pablo Isaza Report

0

The previous answers work sure.

From a coding perspective Blazor rendors the dom it does not manipulate the dom.

The problems with the javascript/css aproach in a blazor app are:

  • You have to set up the javascript.
  • You render stuff into the dom that does not need to be there. This has runtime costs in both Blazor and the Browser. If the "Configurations" use components or have blazor logic they are all going to be instanced/bindings configured ect. even if "hidden".

A Blazor C# way to do this is:

@page "/"
@using static Index.CarEngines

<PageTitle>Index</PageTitle>

<EditForm Model=@model >
    <InputSelect @bind-Value=model.Engines >
        <option value=@One >One</option>
        <option value=@Two >Two</option>
        <option value=@Three >Three</option>
        <option value=@Four >Four</option>
    </InputSelect>
</EditForm>

@switch (model.Engines)
{
    case One:
        <div>One Engine Configuration</div>
        break;
    case Two:
        <div>Two Engine Configuration</div>
        break;
    case Three:
        <div>Three Engine Configuration</div>
        break;
    case Four:
        <div>Four Engine Configuration</div>
        break;
}
@code {

    SomeModel model = new();

    public class SomeModel 
    {
        public CarEngines Engines { get; set; } = One;
    }

    public enum CarEngines
    {
        One = 1,
        Two,
        Three,
        Four
    }
}

Run this and compare what is in the dom...

Obviously the configuration html inside each case block will add complexity, break these out into separate blazor components. This design will make that a lot simpler as well.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!