Search code examples
jqueryblogger

how to get unique id on each blogger post


i am trying to move the post image from the div element to another div with jquery "prependTo" fiddle

$('.post img').prependTo('.move');

because I use a class selector, then each image will be moved and piled on each element of the 'move' class

<div class='post-body' expr:id='"post-body-" + data:post.id'>
<div class='move'/>
<div class='post'>
    aaa aaa aaa
    <img src="image-1"/>
    bbb bbb bbb
</div>
</div>

<div class='post-body' expr:id='"post-body-" + data:post.id'>
<div class='move'/>
<div class='post'>
    ccc ccc ccc
    <img src="image-2"/>
    ddd ddd ddd
</div>
</div>

<div class='post-body' expr:id='"post-body-" + data:post.id'/>
<div class='move'/>
<div class='post'>
    eee eee eee
    <img src="image-3"/>
    fff fff fff
</div>
</div>

it seems that I should get a unique id of each post, or is there a solution to this problem?


Solution

  • You don't need post id, Try this

    $('.post-body').each(function () {
     		$(this).find('.post img').prependTo($(this).find('.move'));
     	});
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class='post-body' expr:id='"post-body-" + data:post.id'>
    <div class='move'></div>
    <div class='post'>
        aaa aaa aaa
        <img src="image-1"/>
        bbb bbb bbb
    </div>
    </div>
    
    <div class='post-body' expr:id='"post-body-" + data:post.id'>
    <div class='move'></div>
    <div class='post'>
        ccc ccc ccc
        <img src="image-2"/>
        ddd ddd ddd
    </div>
    </div>
    
    <div class='post-body' expr:id='"post-body-" + data:post.id'>
    <div class='move'></div>
    <div class='post'>
        eee eee eee
        <img src="image-3"/>
        fff fff fff
    </div>
    </div>