Home > OS >  how to add onclick event on @Html.RadioButtonFor?
how to add onclick event on @Html.RadioButtonFor?

Time:09-26

My radio button basically has a model value.

How can I add an click-eventhandler on @Html.RadioButtonFor ?

@Html.RadioButtonFor(
  model => model.AnswerResponse, 
  true, 
  new { id = "Yes"}, 
  new { onclick = "alert('hi')" 
})

@Html.RadioButtonFor does not contain four arguments.

CodePudding user response:

You can pass the onClick handler using the onclick html attribute

@Html.RadioButtonFor(
  model => model.AnswerResponse, 
  true, 
  new { @tabIndex = 1, @id = "Yes", @onclick="alert('hi')" }
)

CodePudding user response:

if your button is the only one and has a class(or an id by switch . to #), you can do :

const buttonThatIWantToCall = document.querySelector('.MyElementClass')

This calls the element in a variable. Then, you can verify whenever the element in this variable is called with :

buttonThatIWantToCall.addEventListener('click', Function);

then, just do whatever you want to do in the function.

  • Related