You can also create your html in jQuery such that you can bind your event at the time:
$('<a>').attr({'id':'id_1','href':'#'}).addClass('test2').text('test again').click(function(){ event.preventDefault(); alert($(this).attr("id")); }).appendTo('#testing'); On Mar 7, 5:19 pm, brian <bally.z...@gmail.com> wrote: > On Sat, Mar 7, 2009 at 5:31 PM, Miguel <miguelarc...@gmail.com> wrote: > > > Here's what I'm trying to do: > > > Jquery: > > ************* > > $(".test").click(function(event){ > > > event.preventDefault(); > > > $("#testing").html('<a href="#" class="test2" id="1">test again</ > > a>'); > > > }); > > > $("#test2").click(function(event){ > > > event.preventDefault(); > > > alert($(this).attr("id")); > > > }); > > It's probably just a typo but you refer to class test2 above but your > selector here refers to an ID test2. > > > html: > > ************** > > <div> > > <a href="#" class="test" id="1">test</a> > > </div> > > > <div id="testing"></div> > > ***************** > > > Am I going about this all wrong? > > You need to re-bind the event handler on #test2 (or .test2) because > it/they didn't exist when the page loaded. > > http://docs.jquery.com/Frequently_Asked_Questions#Why_do_my_events_st... > > Also note that 1.3.x has a live() function that will do what you want.