tags:

views:

44

answers:

2

Hi, I m creating Divs on the Fly .If i try to get the Attribute id of those Divs ..I m not able to get it..Please suggest me...

 $("#displayPanel div").click(function (){ alert($(this).attr("id")); } 

<div id="displayPanel" class="displayPanel"> 
  <div id="heading"> Display Panel </div> <br/> 
  <div id="save" class="saveClass"></div> <br/> 
   <div id="field1" class="my"> </div> 
   <div id="field2" class="my"> </div>

Field1 and FIled2 are created and appended to the DisplayPanel ..And i m not able to get the id of those DIvs..But i m getting the id for those other existing DIvs

+1  A: 

You need to use the live event, the .click() only works with the elements that are on the page since it got loaded.

$("#displayPanel div").live("click", function(){ ...
fmsf
+2  A: 

Did you re-bind the event after adding the divs?

You could use the live() method to bind live click events:

$("#displayPanel div").live("click", function (){ alert($(this).attr("id")); }
Philippe Leybaert