Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

182
Vistas
How To set a Blazor Input Control via Javascript

I try to set a Blazor InputCheckbox via javascript and expect that the Databinding is executed and the value is written to the Model. But to no avail. What am I doing wrong?

// blazor

<EditForm Model="@Model" OnValidSubmit="@HandleValidSubmit" >
    <InputCheckbox @bind-Value="Model.Checked" class="tripCheckbox" />
</EditForm>


// javascript

const tripCheckBox = container.querySelector('.tripCheckbox');
if (tripCheckBox.checked === false) {
    tripCheckBox.setAttribute('checked', 'checked');
    tripCheckBox.checked = true;

} else {
    tripCheckBox.removeAttribute('checked');
    tripCheckBox.checked = false;
}
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

I think that you want to execute your js in some event:

<InputCheckbox @bind-Value="Model.Checked" class="tripCheckbox" @onclick=OnMyCheckboxClick />
 // Code behind
 [Inject]
 private IJRunTime Js {get; set;}
 private async task OnMyCheckboxClick() { 
   Js.InvokeVoidAsync("YourJSMethod");
 }
about 4 years ago · Juan Pablo Isaza Denunciar

0

The problem in changing the value with JS is the Blazor events don't get called. You have to invoke the DOM events yourself.

Here's a demo page and code, using most of your code:

My JS function loaded in _layout.cshtml.

    <script>
        window.SetMyCheckBox = function()
        {
            const tripCheckBox = document.getElementById('tripCheckbox');
            if (tripCheckBox.checked === false) {
                tripCheckBox.setAttribute('checked', 'checked');
                tripCheckBox.checked = true;
                var event = new Event('change');
                tripCheckBox.dispatchEvent(event);
            } else {
                tripCheckBox.removeAttribute('checked');
                tripCheckBox.checked = false;
                var event = new Event('change');
                tripCheckBox.dispatchEvent(event);
            }
        }
    </script>

And this is a test page:

@page "/"
@inject IJSRuntime JS

<PageTitle>Index</PageTitle>

<h1>Hello, world!</h1>

<EditForm Model="@model">
    <InputCheckbox @bind-Value="model.Checked" id="tripCheckbox" />
</EditForm>

<div class="m-2">
    Checked: @model.Checked
</div>

<div class="m-2">
    <button class="btn btn-primary" @onclick="Clicked"> Change </button>
</div>


@code {
    private Model model = new Model();

    class Model
    {
        public bool Checked { get; set; }
    }

    private async Task Clicked()
    {
      await JS.InvokeVoidAsync("SetMyCheckBox");
    }
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda