0

I am trying to find if the Check Box is checked or not. I am sure I am writing the write code but it's not working. Following is my code:

@Html.CheckBoxFor(m => m.Country, new { id = "country" })

And my JavaScript Code:

$(document).ready(function () {

  if ($("#country").is(":checked"))
  {
     alert("Working");
  }

});
Armaan Labib
  • 115
  • 1
  • 12
  • can you check html element? is it have a checkbox that has a "country" id attribute – hasan Jun 03 '17 at 20:44
  • Not so much code so I cannot tell if the checkbox will always be unchecked when the page loads? If that is the case it won't trigger alert since the function only runs when the document has been loaded – user2782999 Jun 03 '17 at 21:02

2 Answers2

0

perhaps try jquery on load instead of on document ready:

$(document).on({
  load: function () {
       if ($("#country").is(":checked")){
          alert("Working");
       }
  }
});
SilverSurfer
  • 3,810
  • 2
  • 17
  • 36
Sam0
  • 1,429
  • 1
  • 11
  • 13
  • I am trying `onchange` event but not working. Its not reading the id of country. I might need to use something like this: `$("input:checkbox#country")` – Armaan Labib Jun 04 '17 at 07:04
0

Make sure the checkbox is actually checked when the document has loaded, if not the if state will return false and it runs only once. You could always try with

$('#country').prop('checked')

Also here is another thread which provides multiple solutions Get checkbox value in jQuery

user2782999
  • 305
  • 1
  • 5
  • 20