0

I am injecting HTMl code into the dom using Ajax. It does this sucessfully but my jQuery selector does not work for this HTML input

So if i do the following in my Jquery it does not get recognized...

$("input[id*='cb_Compare']").click(function ()

However if i assign the input with an onclick function in my server side code thatworks.

What is this sorcery?

MarsOne
  • 2,155
  • 5
  • 29
  • 53

3 Answers3

3

If the DOM is added dynamically, you need to use event delegation. Read this link for further understanding. Event Delegation

$(document).on('click',"input[id*='cb_Compare']",function (){
  //your code
});
Norlihazmey Ghazali
  • 9,000
  • 1
  • 23
  • 40
2

Use event delegation with immediate parent selector is best way to do dynamically created elements

$("#parentSelector").on("click", "input[id*='cb_Compare']" , function() { 

          // your code here
});

or

$(document).on("click", "input[id*='cb_Compare']" , function() { 

              // your code here
    });
Sudharsan S
  • 15,336
  • 3
  • 31
  • 49
1

Since you are generating HTML dynamically with jQuery so you have to use event delegation in order to bind the click() event as shown :-

$(document.body).on('click','input[id*="cb_Compare"]',function(){
  //...your stuff
});
Kartikeya Khosla
  • 18,743
  • 8
  • 43
  • 69