Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Html.Checkbox onchange event

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.

like image 991
John Elizabeth Avatar asked Jul 11 '14 14:07

John Elizabeth


People also ask

Can you use Onchange on checkbox?

Definition and Usage The onchange event occurs when the value of an element has been changed. For radiobuttons and checkboxes, the onchange event occurs when the checked state has been changed.

What event is fired when a checkbox is checked?

There are two events that you can attach to the checkbox to be fired once the checkbox value has been changed they are the onclick and the onchange events.

How do you handle a checkbox in HTML?

The <input type="checkbox"> defines a checkbox. The checkbox is shown as a square box that is ticked (checked) when activated. Checkboxes are used to let a user select one or more options of a limited number of choices. Tip: Always add the <label> tag for best accessibility practices!

How check if checkbox is changed?

var val = jQuery("#frameName"). contents(). find(":checkbox").is(":checked");


1 Answers

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

like image 177
Dhaval Marthak Avatar answered Oct 17 '22 15:10

Dhaval Marthak