1

I'm building a string that will be place in a div pro grammatically. I am trying to call the onclick attribute of the input checkbox and having a little bit of trouble. I am trying to pass a unique value id with each checkbox click. The code below is what I'm using. See below for the problem:

var count = 1;
$.each(JSON.parse(data.d), function (k, v) {
    var searchName = v.searchName;
    resultString += "<div class='row form-group'>";
    resultString += "<div class='col-sm-1 right-content'><input type='checkbox' onclick = 'authorCheckboxclick(this)' id='" + searchName + "'></div>";
    resultString += "<div class='col-sm-11'>";
    resultString += "<span>";
    //resultString +=  v.text   
    resultString += count + ". " + v.text
    resultString += "</span>";
    resultString += "<br />";
    resultString += "<span>";
    //resultString += "Consectetur adipisicing, Consequatur, 2015.";
    resultString += "</span>";
    resultString += "</div>";
    resultString += "</div>";
    //resultString += "<br><br>";
    count++;
});

In the authorCheckboxclick function if I put var answerid = $(this).attr('id'); I get undefined.

function authorCheckboxclick(elem) {
    var answerid = $(this).attr('id');
    alert(answerid);  //I get undefined
    var answerid = $(this).attr(elem.id);
    alert(answerid); //I get undefined
    var answerid = $(this).attr(elem.id);
    alert(answerid); //I get undefined
    var searchTerm = elem.id;
    alert(searchTerm); //I get proper value
    searchTerm = searchTerm.substring(0, 3);
    alert(searchTerm); //I get proper value
    var answerid = $(this).attr(elem.id);
    alert(answerid); //I get undefined


    var search = searchTerm.toUpperCase();
    var array = jQuery.grep(dataMembers, function (value) {
        return value.toUpperCase().indexOf(search) >= 0;
    });

Is there a reason my jQuery call is not working and my JavaScript is? Is there a best practice to send the id value to a junction? Am I mixing Apples with Orange? Which method show be faster?

2
  • 1
    Possible duplicate of jQuery $(this).attr("id") not working Commented Nov 18, 2016 at 16:16
  • Thanks-that works-- But is there a better way? Commented Nov 18, 2016 at 16:21

1 Answer 1

1

The immediate solution to your problem is that you're using the this keyword in your function. As you are calling the function from an on* attribute the scope of the function will be the window, not the element that raised the event. To fix this, simply use the element you provide in the parameter, ie. $(elem) instead of $(this).

A better solution entirely would be to use an unobtrusive delegated event handler which can utilise the this keyword as you're attempting to. It also has the benefits of leaving cleaner HTML code and being a better separation of concerns. Try this:

var count = 1;
$.each(JSON.parse(data.d), function(k, v) {
  var searchName = v.searchName;
  resultString += '<div class="row form-group">' +
    '<div class="col-sm-1 right-content"><input type="checkbox" id="' + searchName + '"></div>' +
      '<div class="col-sm-11">' +
        '<span>' + count + ". " + v.text + '</span><br />' +
        '<span></span>' +
      '</div>' +
    '</div>';
  count++;
});

$(document).on('change', '.row :checkbox', function() {
  var answerid = this.id;
  var search = searchTerm.toUpperCase();
  var array = jQuery.grep(dataMembers, function(value) {
    return value.toUpperCase().indexOf(search) >= 0;
  });
});
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks- I'll work it into my code. Correct me if I'm wrong but this will be called for every checkbox in my entire application (Single page app) . How can I determine which popup div it's coming from? I use a lot of checkboxes within the app that call different functions
In that case you just need to make the .row :checkbox selector specific to the require page, ie #profilePage .row :checkbox
Should I preface my ID's so I know that they come from a special location? Like id= "publication"_+ ID value and id= "author" + ID Value
hate to be a pest but could #profilePage be #profileDIV? and still work
You could do that - although I'd suggest using a common class over an id
|

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.