mkotwd
mkotwd

Reputation: 21

how refresh the document ready in jquery

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 ..

Upvotes: 2

Views: 12854

Answers (3)

Loktar
Loktar

Reputation: 35309

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

Use Delegate

Reference

Delegate vs Live

Demo

Upvotes: 6

LesterDove
LesterDove

Reputation: 3044

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.

Upvotes: 1

Justin Thomas
Justin Thomas

Reputation: 5848

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

Upvotes: 1

Related Questions