0

Good day.

For example i have code:

$("#button").on("click",function(){alert("hello");})

<button id="button">Test</button>

if this code find on page all working, but if we upload button use ajax, jquery for #button not work and he work if i upload his only with button use ajax...

But i would be write all jquery scripts for page in a separate file...

How make that script jquery work for element which we add in body page use jquery ?

  • Don't show your working code, show your problematic code and explain what you need help with – andrew Sep 22 '13 at 10:35
  • possible duplicate of [Direct vs. Delegated - jQuery .on()](http://stackoverflow.com/questions/8110934/direct-vs-delegated-jquery-on) – hjpotter92 Sep 22 '13 at 10:39
  • i have answer but i cannt add him 7 hourse... –  Sep 22 '13 at 10:51

2 Answers2

0

as @hjpotter92 pointed out, with ajax added content you can use delegation. So attach the havent handler to the container where you upload your button and specify a class for example to catch the right event.

$("#container").on("click", ".my-button", function() { alert("hello"); })
th3n3rd
  • 375
  • 1
  • 3
  • 20
0

Case 1 (direct):

$("#button").on("click", function() {...});

Case 2 (delegated):

$("body").on("click", "#button", function() {...});