53

I have this code:

$.ajax({
        url : url,
        data : {ids : JSON.stringify(jsonids), hotel_id: hotel_id},
        success : function(response)
        {
            $('#be-images ul').prepend(response).fadeIn('slow');
        },
        dataType: 'html'
    });

but the fade In does not work...I want the content to be prepended and faded in...how will I do this?

Thanks in advance!

3 Answers 3

112

Assuming response is HTML then try this:

$(response).hide().prependTo("#be-images ul").fadeIn("slow");

When you do it this way:

$('#be-images ul').prepend(response).fadeIn('slow');

the thing you're actually fading in is the result of the initial selector (the list at the front), which is already visible.

2
  • both cletus and nick, your ways are awesome.. i stumbled upon the same problem and both of your ways work like a breeze... thanks...
    – coder101
    Commented Feb 11, 2013 at 14:12
  • @cletus : I've same problem but my data is json array. I uses .each function and trying to prepend but it doesn't show any data prepending - var li = '<li>'+item.img+'</li>';$('.twitter-list').prepend(li).hide().fadeIn("fast");
    – Vilas
    Commented Jun 7, 2016 at 5:10
42

+1 to cletus, but I just wanted to highlight the other way you could do it.

$('#be-images ul').prepend(
    $(response).hide().fadeIn('slow')
);
1
  • this works well cause it doesn't hide the element to prepend to, only the element being prepended
    – lukeaus
    Commented Oct 24, 2016 at 18:00
1

Try this: HTML

<button>Add</button>
<div id="data"></div>

Jquery:

$('button').click(function() {
  $('#data').prepend('<div class="item">Test</div>'"');
    $("#data .item:first-child").hide();
   $("#data .item:first-child").fadeIn();
});

Live Demo: jsfiddle

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.