fon
fon

Reputation: 265

how to i access dynamically create div or elements with jquery

I have made a ajax request and the various rows gotten I echo each into a dynamically created the div. Now I want to bind a event to each of these divs like mousedown() .. do something but I am not able to access any of the divs. Please can any one help me with that?

Upvotes: 0

Views: 1087

Answers (3)

max
max

Reputation: 101811

if your using jQuery 1.7+ you should use .on() or .delegate() as .live() is depreciated.

jQuery("table").on("click", "tr", function(event){});

Upvotes: 3

Simon
Simon

Reputation: 23141

and another solution, than .live()

You can just load the jquery function dinamycally, in your ayax request. it's not usefull in small tasks, but in large projects it can be very usefull (from my practice...)

Upvotes: 0

Nick Craver
Nick Craver

Reputation: 630349

You can use .live(), like this:

$(".myDivClass").live('mousedown', function() {
  alert('Your mouse is down!');
});

You can view a quick demo here

It doesn't bind an event to those new divs, it just executes the function/handler whenever a mousedown happens in an element matching that class (determined via bubbling), getting the effect you want...as if you bound the handler to each new div that appears.

Upvotes: 2

Related Questions