Skip to content Skip to sidebar Skip to footer

Jquery Prepend To Crash

If I ajax any input into my page the following code works but if I print some specific code I get an exception, alert test3 never fires. alert('test2'); $(html).hide().prependTo('#

Solution 1:

I just ran into this issue after upgrading from jquery 1.7 to 1.9. This is a bug in the implementation of fadeIn().

It has to do with the fact that the data() method now returns undefined rather than null when you query for a key that doesn't exist.

Anyhow, a workaround that I found is to prepend the data first, and then get a jquery handle to the new, prepended content, and fade that in.

Since you're inserting the new content into a table, it's a little annoying to find the thing you just inserted, since it's not actually the first child (it's wrapped in a "tbody" tag implicitly). I worked around this by setting a temporary attribute on the inserted thing, searching for it, and then removing it. This is a gigantic hack obviously, but it does work.

var html = "<tr><td>new thing1</td><td>new thing2</td>";

$("#clickme").click(function () {
    var target = $("#current table.vtable");
    $(html).attr("jquery_workaround","sigh").hide().prependTo(target);
    var newstuff = target.find("[jquery_workaround=sigh]");
    newstuff.removeAttr("jquery_workaround").fadeIn("slow");
});

For the fiddling: http://jsfiddle.net/BUVUj/

Post a Comment for "Jquery Prepend To Crash"