user823527
user823527

Reputation: 3712

Adding element at top of div

I'd like to add an element to a container div before existing elements. I have attempted to this three different ways and couldn't

Method 1

$('.channel:first',$('#scroll_5')).before(print_nav);

Method 2

$('.channel:eq(0)',$('#scroll_5')).before(print_nav);

Method 3

$('.channel',$('#scroll_5')).each(function () {
    $(this).before(print_nav);
    return false;
});

The container div is scroll_5 and has elements with the class channel. I want to add a new channel at the top. What is the right way to do that?

Upvotes: 0

Views: 113

Answers (2)

defuz
defuz

Reputation: 27611

$('#scroll_5').prepend(print_nav);

See http://api.jquery.com/prepend/

Or:

$(print_nav).prependTo('#scroll_5');

See http://api.jquery.com/prependTo/

Upvotes: 5

Dan Lister
Dan Lister

Reputation: 2583

Try...

$("#scroll_5").prepend("<div />");

Upvotes: 0

Related Questions