Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - Moving a div to the top of it's parent

Tags:

jquery

I've got a script where if a div exists I want to be at the top of it's parent div. Here's what I tried:

        if ($(".pane-bundle-hblock .field-name-field-hblock-image-right").length){
            $(".pane-bundle-hblock .field-name-field-hblock-image-right").parent().prepend($('this'));
        }

What did I get wrong?

like image 703
Matt Coady Avatar asked Dec 13 '13 00:12

Matt Coady


Video Answer


1 Answers

$('this') selects <this></this> elements.

I would do something like this:

$(".pane-bundle-hblock .field-name-field-hblock-image-right").each(function() {
    $(this).parent().prepend(this);
});

If the element doesn't exist, the .each() won't have anything to iterate over, so you don't really need to check to see if it exists.

like image 141
Jason P Avatar answered Sep 20 '22 23:09

Jason P