18

I tried below event in order to reach onchange of checkbox.

 @Html.CheckBox("AutoCalculateMandate", true , new { onchange = "AutoCalculateMandateOnChange" })

Javascript:

function AutoCalculateMandateOnChange() {
    alert("working");
}

When i try above javascript code , alert never displays nothing(not working).

How can i enable/disable below input on Html.Checkbox value changed ?

<input type="text" id="LevyFee" class="form-control" data-required="true" ">

Any help appreciates.

Thanks.

5
  • 3
    It should be onchange = "AutoCalculateMandateOnChange()" instead onchange = "AutoCalculateMandateOnChange"! Commented Jul 11, 2014 at 14:53
  • how can i get selected value of checkbox and enable/disable input ? thanks also please answer i will accept best answer Commented Jul 11, 2014 at 14:55
  • I'm a bit late to the party, but can someone tell me what language/framework the @HTML is? I've never seen this before in javascript, so just curious. Commented Dec 14, 2018 at 21:59
  • @JonathanM This is ASP.NET's Razor syntax - Html is an instance of type HtmlHelper, and the @ sign allows for inline code inside markup. Commented Nov 23, 2019 at 22:24
  • @Oliver, thanks. I've added ASP.NET to the tag list. Commented Nov 25, 2019 at 22:56

1 Answer 1

33

You can get checkbox as a element in your function by passing this as a reference see updated markup below

<input type="checkbox" value="check" id="AutoCalculateMandate" onchange = "AutoCalculateMandateOnChange(this)"/>
<label for="AutoCalculateMandate">
    Auto Calculate
</label> <br />

Since you're using MVC so it can be achieved like this:

 @Html.CheckBox("AutoCalculateMandate", true , new { onchange = "AutoCalculateMandateOnChange(this)" })

javascript

function AutoCalculateMandateOnChange(element){
     document.getElementById("LevyFee").disabled = element.checked;    
}

Demo

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.