dsb
dsb

Reputation: 2517

jQuery attribute selector does not work

I have the following element in my html:

<button id="btn-confirm-cancel-22" type="button" class="font-size-large" data-toggle="modal" data-target="#cancelProposalConfirationModal" data-cancel-rid="22">Cancel</button>

And this jQuery function:

$("[data-cancel-rid]").on('click', function (event) {
    var request_id = $(this).attr('data-cancel-rid');
    console.log(request_id);
});

When clicking the button the browser does not call the jQuery function. Why???

Upvotes: 0

Views: 54

Answers (1)

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167172

Either wrap the whole function inside, also could be because of non-delegation:

$(function () {
  $("body").on('click', "[data-cancel-rid]", function (event) {
    var request_id = $(this).attr('data-cancel-rid');
    console.log(request_id);
  });
});

Or load the code in the end of the document, before </body> tag. When the code executes, may be the document is not loaded.

Upvotes: 1

Related Questions