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?
$('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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With