Take a look at this:

http://docs.jquery.com/Frequently_Asked_Questions#Why_doesn.27t_an_event_work_on_a_new_element_I.27ve_created.3F

That contains a short answer to your question:

"Events are bound only to elements that exist when you issue the jQuery
call. When you create a new element, you must rebind the event to it."

The longer answer and a couple of workarounds can be found here:

http://docs.jquery.com/Frequently_Asked_Questions#Why_do_my_events_stop_working_after_an_AJAX_request.3F

- Richard

On Mon, Dec 1, 2008 at 11:41 PM, dave <[EMAIL PROTECTED]> wrote:

>
> Hey all,
> I have just started using JQuery and have come across a hurdle.  I am
> creating DIVs on the fly (using a JQuery).  For these DIVs, I have
> them tagged to a Class.  For this Class, I have an Event tied to it.
> For some reason, the Event doesn't seem to work for the newly created
> DIVs.  However, it works for the hard-coded DIVs.
>
> Here's an example of what I mean.
>
> I'm using a Javascript to create the DIVs on the fly.
> e.g.
>
> function createDiv() {
>    var divHtml = $("#existingDivs").html();
>    $("#existingDivs").html( divHtml +
>        "<div class=classTiedToEvent>ClassTiedtoEvent class test</
> div>" );
> }
>
> I then have an Event tied to the class, "classTiedToEvent," like so...
> e.g.
>
> $(document).ready(function(){
>    $("div").click(function(){
>      if ( $(this).hasClass("classTiedToEvent") )
>        $(this).animate({ left: -10 }, 75)
>               .animate({ left: 10 }, 75)
>               .animate({ left: -10 }, 75)
>               .animate({ left: 10 }, 75)
>               .animate({ left: 0 }, 75);
>    });
> });
>
> or...
>
> e.g.
>
> $(document).ready(function(){
>    $(".classTiedToEvent").click(
>        function() {
>            alert("clicked");
>        }
>    );
> });
>
> I've tried variations of the 2 above functions and neither have
> worked.  However, they do work if I were to hardcode the
> "classTiedToDiv" DIV in my HTML.
>
> The DIVs that I generate with my first Javascript function
> ("createDiv") do have the correct Class associated with them (I
> think), because they take on the attributes I've defined in my
> stylesheet.
>
> Any ideas what I'm doing wrong?
>
> Please let me know if you'd like me to clarify.  Thanks.
>
> Dave
>

Reply via email to