Raj
Raj

Reputation: 204

Not getting the check box event after click on checkbox

I am not getting the checked status of a checkbox. Checks are dynamically created. Below is the code:

'[id^=taskcheckbox_] click' : function(el){
    var taskObj={};
    var taskCheckBoxId = el.attr("id");
    var indexOfTask = taskCheckBoxId.split("_")[2];
    var checkedValue = this.element.find("#"+taskCheckBoxId).attr("checked");
    var task_id = taskCheckBoxId.split("_")[1];
    if(checkedValue == "checked" && checkedValue != undefined){
        this.count++;
}}

Control is not going in if condition after the check box is checked. Value of checkedValue is always coming as undefined.

Upvotes: 1

Views: 64

Answers (1)

Rino Raj
Rino Raj

Reputation: 6264

Use prop() instead of attr()

►If you're using jQuery 1.6 or later, use prop() instead of attr()

var checkedValue = this.element.find("#"+taskCheckBoxId).prop("checked");

When you are using prop(), the value returned will be Boolean

$(document).delegate("[id^=taskcheckbox_]", "click", function(el) {
  //[id^=taskcheckbox_] click' : function(el){
  var taskObj = {};
  var taskCheckBoxId = el.currentTarget.id;
  var indexOfTask = taskCheckBoxId.split("_")[2];
  var checkedValue = $(el.currentTarget).is(":checked");
  var task_id = taskCheckBoxId.split("_")[1];
  if (checkedValue) {
    console.log('checked')
      //this.count++;
  }
})

DEMO FIDDLE

Upvotes: 3

Related Questions