2

for example if i write this code :

$(document).ready(function(){

    $("div#i").click(function(){
        alert("ok");
       });
    });  

and the div element with the i id is not found in the page when load , so after i create it with some function ; and i click it , nothing happen , so how i can refresh the ready function in jquery ..

Pointy
  • 405,095
  • 59
  • 585
  • 614
mkotwd
  • 21
  • 1
  • 2
  • 5

3 Answers3

6
$(document).delegate('div#i','click',function(){});

Use Delegate

Reference

Delegate vs Live

Demo

Community
  • 1
  • 1
Loktar
  • 34,764
  • 7
  • 90
  • 104
  • 1
    I'd recommend using [`.delegate`](http://stackoverflow.com/questions/4204316/jquery-live-vs-delegate/4204349#4204349) – Raynos Mar 28 '11 at 18:11
  • @Raynos thank you for the info, answer changed, and linked to the answer you shared with me as to why you should use delegate over live. – Loktar Mar 28 '11 at 18:19
1

Look into using .live() : http://api.jquery.com/live/

Justin Thomas
  • 5,680
  • 3
  • 38
  • 63
1

Take that click event handler out of (document).ready and place it in the function that you've mentioned which creates the ID.

function someFunction()
{
  //creates the id, then
    $("div#i").click(function(){
        alert("ok");
       });

}

EDIT:

If this is just one example among many potential event handlers, then wrap them all in a single function, say, bindEvents() and you can call that every time the page is 'dirtied' by an ID creation.

But the other commenters' approach of using .live() will probably be less to maintain, and it's "the JQuery way" if that's a concern for you.

LesterDove
  • 3,014
  • 1
  • 23
  • 24